Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: no pass-values calling?

Reply
Thread Tools

Re: no pass-values calling?

 
 
Neil Cerutti
Guest
Posts: n/a
 
      01-16-2008
On Jan 15, 2008 10:09 PM, J. Peng <(E-Mail Removed)> 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 <(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
cokofreedom@gmail.com
Guest
Posts: n/a
 
      01-16-2008
On Jan 16, 1:21 pm, "Neil Cerutti" <(E-Mail Removed)> wrote:
> On Jan 15, 2008 10:09 PM, J. Peng <(E-Mail Removed)> 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 <(E-Mail Removed)>


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
 
Reply With Quote
 
 
 
 
Neil Cerutti
Guest
Posts: n/a
 
      01-16-2008
On Jan 16, 2008 7:58 AM, <(E-Mail Removed)> wrote:
> On Jan 16, 1:21 pm, "Neil Cerutti" <(E-Mail Removed)> 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 <(E-Mail Removed)>
 
Reply With Quote
 
cokofreedom@gmail.com
Guest
Posts: n/a
 
      01-16-2008
>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments