[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: no pass-values calling?

Neil Cerutti

1/16/2008 12:21:00 PM

On Jan 15, 2008 10:09 PM, J. Peng <peng.kyo@gmail.com> wrote:
> Hello,
>
> I saw this statement in Core Python Programming book,
>
> All arguments of function calls are made by reference, meaning that
> any changes to these parameters within the function
> affect the original objects in the calling function.

Yes, that's generally correct. But you must be careful about what is
meant by "changes to parameters". Assigning a new value to a parameter
name (inside the function, a parameter is just a local variable) does
not change the original object--it only rebinds the local variable to
a new object.

In the following function, a is rebound with an assignment statement,
while b is mutated, i.e., changed, with an assignment statement.

def f(a, b):
a = 12
b.value = 14

Argument a will never be changed, while argument b will be. Python's
argument passing semantics are extremely simple. It's the assignment
statement that's tricky: some assignments mutate/change objects, and
some only rebind names.

> Does this mean there is not pass-values calling to a function in
> python? only pass-reference calling? Thanks!

Neither is quite true. Values are passed by binding parameter names to
their corresponding arguments. This is similar to pass-by-reference in
some cases (when the argument is mutated) but not in others (when the
argument is not mutated). Thinking of it as pass-by-reference may help
you to understand it, but bear in mind that Python's "references" may
be rebound to new objects, which is quite different from the usual
behavior of references.

--
Neil Cerutti <mr.cerutti+python@gmail.com>
3 Answers

cokofreedom

1/16/2008 12:59:00 PM

0

On Jan 16, 1:21 pm, "Neil Cerutti" <mr.ceru...@gmail.com> wrote:
> On Jan 15, 2008 10:09 PM, J. Peng <peng....@gmail.com> wrote:
>
> > Hello,
>
> > I saw this statement in Core Python Programming book,
>
> > All arguments of function calls are made by reference, meaning that
> > any changes to these parameters within the function
> > affect the original objects in the calling function.
>
> Yes, that's generally correct. But you must be careful about what is
> meant by "changes to parameters". Assigning a new value to a parameter
> name (inside the function, a parameter is just a local variable) does
> not change the original object--it only rebinds the local variable to
> a new object.
>
> In the following function, a is rebound with an assignment statement,
> while b is mutated, i.e., changed, with an assignment statement.
>
> def f(a, b):
> a = 12
> b.value = 14
>
> Argument a will never be changed, while argument b will be. Python's
> argument passing semantics are extremely simple. It's the assignment
> statement that's tricky: some assignments mutate/change objects, and
> some only rebind names.
>
> > Does this mean there is not pass-values calling to a function in
> > python? only pass-reference calling? Thanks!
>
> Neither is quite true. Values are passed by binding parameter names to
> their corresponding arguments. This is similar to pass-by-reference in
> some cases (when the argument is mutated) but not in others (when the
> argument is not mutated). Thinking of it as pass-by-reference may help
> you to understand it, but bear in mind that Python's "references" may
> be rebound to new objects, which is quite different from the usual
> behavior of references.
>
> --
> Neil Cerutti <mr.cerutti+pyt...@gmail.com>

So basically the scope is the reason for confusion a lot of the time?

def some_name():
alist = [5]
bint = 5
cstring = '5'
ddictionary = {0:5}

def other_name(alist, bint, cstring, ddictionary):
alist = 4
bint = 4
cstring = '4'
ddictionary = 4
print "other_name:",
print alist, bint, cstring, ddictionary

def another_name(alist, bint, cstring, ddictionary):
alist[0] = 3
# bint cannot be changed it is immutable
# cstring cannot be changed it is immutable
ddictionary[0] = 3
print "another_name:",
print alist, bint, cstring, ddictionary

another_name(alist, bint, cstring, ddictionary)
other_name(alist, bint, cstring, ddictionary)
print "our entries are now:",
print alist, bint, cstring, ddictionary

Neil Cerutti

1/16/2008 1:14:00 PM

0

On Jan 16, 2008 7:58 AM, <cokofreedom@gmail.com> wrote:
> On Jan 16, 1:21 pm, "Neil Cerutti" <mr.ceru...@gmail.com> wrote:
> > In the following function, a is rebound with an assignment statement,
> > while b is mutated, i.e., changed, with an assignment statement.
> >
> > def f(a, b):
> > a = 12
> > b.value = 14
> >
> > Argument a will never be changed, while argument b will be. Python's
> > argument passing semantics are extremely simple. It's the assignment
> > statement that's tricky: some assignments mutate/change objects, and
> > some only rebind names.
>
> So basically the scope is the reason for confusion a lot of the time?

No, my hypothesis is that Python's assignment statement semantics are
the tricky part--once you understand them, the utter simplicity of
Python's argument passing semantics will be evident.

--
Neil Cerutti <mr.cerutti+python@gmail.com>

cokofreedom

1/16/2008 1:43:00 PM

0

>
> No, my hypothesis is that Python's assignment statement semantics are
> the tricky part--once you understand them, the utter simplicity of
> Python's argument passing semantics will be evident.

Indeed, I find the simple nature of it and the fact things tend not to
change is extremely useful. It is an advantage to have rules to abide
by that cannot be ignored or by-passed.