Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #6687
| References | <F8395F78-615E-4FBD-B6FC-1D6173EAEA45@mcgill.ca> <F4EAD1ED-563D-4D6E-A50C-68308A9F26B7@mcgill.ca> |
|---|---|
| Date | 2011-05-31 12:11 +1100 |
| Subject | Re: scope of function parameters (take two) |
| From | Daniel Kluev <dan.kluev@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2282.1306804280.9059.python-list@python.org> (permalink) |
On Tue, May 31, 2011 at 11:28 AM, Henry Olders <henry.olders@mcgill.ca> wrote:
> What I would like is that the variables which are included in the function definition's parameter list, would be always treated as local to that function
You still mis-reading docs and explanations you received from the list.
Let me try again.
First, there are objects and names. Calling either of them as
'variables' is leading to this mis-understanding. Name refers to some
object. Object may be referenced by several names or none.
Second, when you declare function `def somefunc(a, b='c')` a and b are
both local to this function. Even if there are some global a and b,
they are 'masked' in somefunc scope.
Docs portion you cited refer to other situation, when there is no
clear indicator of 'locality', like this:
def somefunc():
print a
In this - and only in this - case a is considered global.
Third, when you do function call like somefunc(obj1, obj2) it uses
call-by-sharing model. It means that it assigns exactly same object,
that was referenced by obj1, to name a. So both obj1 and _local_ a
reference same object. Therefore when you modify this object, you can
see that obj1 and a both changed (because, in fact, obj1 and a are
just PyObject*, and point to exactly same thing, which changed).
However, if you re-assign local a or global obj1 to other object,
other name will keep referencing old object:
obj1 = []
def somefunc(a):
a.append(1) # 'a' references to the list, which is referenced by
obj1, and calls append method of this list, which modifies itself in
place
global obj1
obj1 = [] # 'a' still references to original list, which is [1]
now, it have no relation to obj1 at all
somefunc(obj1)
--
With best regards,
Daniel Kluev
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: scope of function parameters (take two) Daniel Kluev <dan.kluev@gmail.com> - 2011-05-31 12:11 +1100
csiph-web