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.
Does this mean there is not pass-values calling to a function in
python? only pass-reference calling? Thanks!
No, Python does not use either pass by reference or pass by value. It
uses pass by object. (Sometimes called "pass by object reference".)
See: http://effbot.org/zone/call-by-object.htm
for further details.
--
Steven
Anybody using the terms variable, reference or call-by-value is most
likely explaining Python the wrong way.
Sorry dude :)
Christian
Hi,
How to modify the array passed to the function? I tried something like this:
>>> a
[1, 2, 3]
>>> def mytest(x):
... x=[4,5,6]
...
>>> mytest(a)
>>> a
[1, 2, 3]
As you see, a was not modified.
Thanks!
> Hi,
>
> How to modify the array passed to the function? I tried something like
> this:
>
>>>> a
> [1, 2, 3]
>>>> def mytest(x):
> ... x=[4,5,6]
This line does NOT modify the list [1, 2, 3]. What it does is create a
new list, and assign it to the name "x". It doesn't change the existing
list.
If you have not already done this, you should read this:
http://effbot.org/zone/python-objects.htm
Consider this function:
def test(alist):
alist.append(0) # this modifies the existing list
alist = [1, 2, 3] # this changes the name "alist"
return alist
Now try it:
oldlist = [10, 9, 8, 7]
newlist = test(oldlist)
Can you predict what oldlist and newlist will be equal to?
oldlist will be [10, 9, 8, 7, 0] and newlist will be [1, 2, 3]. Do you
see why?
--
Steven
'a' was not modified because you locally assigned a new object with
'x=[4,5,6]'. If you want the new list you created you will have to
return it. You can see how you modify it if you were to use
'x.append()' or 'x.extend()' for eg.
The term "reference" is fine, since that's exactly how it works. One
gets at an object via some reference, be it a name or some access into
a container object. When an object has no more references to itself,
it becomes a candidate for garbage collection. And so on.
--
\ "I planted some bird seed. A bird came up. Now I don't know |
`\ what to feed it." -- Steven Wright |
_o__) |
Ben Finney
Sounds strange.
In perl we can modify the variable's value like this way:
$ perl -le '
> $x=123;
> sub test {
> $x=456;
> }
> test;
> print $x '
456
Thanks you, but I know exactly how Python works. I'm actually developing
CPython and PythonDotNET. While your description of Python's memory
management is technically, it's just an implementation detail of the
CPython implementation. Jython and IronPython are using different
approaches for GC.
Anyway your message doesn't help a newbie and it gives most certainly
the wrong impression. You are using words that have a different meaning
in other languages. If you explain Python w/o the words variable,
pointer, reference or call-by-value you have a much better chance to
explain it right. Trust me :)
Christian
Ok, thanks.
But there is a following question,when we say,
>>> x=[1,2,3]
we create a list.then we say,
>>> x=[4,5,6]
we create a new list and assign it to x for future use.
How to destroy the before list [1,2,3]? does python destroy it automatically?
> we create a new list and assign it to x for future use. How to
> destroy the before list [1,2,3]? does python destroy it
> automatically?
Yes, Python detects that the older list is no longer in use (if that
is indeed the case), and destroys it automatically. If you're curious
how that works, take a look at
http://en.wikipedia.org/wiki/Reference_counting
> Ben Finney wrote:
> > The term "reference" is fine, since that's exactly how it works.
> > One gets at an object via some reference, be it a name or some
> > access into a container object. When an object has no more
> > references to itself, it becomes a candidate for garbage
> > collection. And so on.
>
> Thanks you, but I know exactly how Python works. I'm actually
> developing CPython and PythonDotNET.
Uh, okay. I didn't ask for you to flash your credentials, but if that
is significant to you, be my guest.
> Anyway your message doesn't help a newbie and it gives most
> certainly the wrong impression. You are using words that have a
> different meaning in other languages. If you explain Python w/o the
> words variable, pointer, reference or call-by-value you have a much
> better chance to explain it right. Trust me :)
I've done my share of explaining of Python to people, and found
"reference" to be exactly the right term to help the newbie understand
what's happening and what they should expect.
I agree with you on "variable", "pointer", and "call-by-value". Those
don't describe how Python works, and thus only confuse the matter.
Thus I avoid them, and correct newbies who appear to be getting
confused because of those existing concepts.
--
\ "I don't accept the currently fashionable assertion that any |
`\ view is automatically as worthy of respect as any equal and |
_o__) opposite view." -- Douglas Adams |
Ben Finney
> Christian Heimes <li...@cheimes.de> writes:
>
>> Dennis Lee Bieber wrote:
>> > Since all "variable" names in Python are references to objects,
>> > anything accessed using a name is accessed by reference.
>>
>> Anybody using the terms variable, reference or call-by-value is most
>> likely explaining Python the wrong way.
>
> The term "reference" is fine, since that's exactly how it works. One
> gets at an object via some reference, be it a name or some access into a
> container object. When an object has no more references to itself, it
> becomes a candidate for garbage collection. And so on.
The term "reference" *by itself* is fine, so long as there is no
possibility of confusion with the very common concept of "call by
reference" (or "pass by reference").
But since the Original Poster himself raised the question of whether
Python is call by reference or call by value, that should be a great big
warning flag to avoid the term "reference" until he's reset his brain.
Python is not C, and if you talk about Python using C terminology without
making it absolutely crystal clear that the semantics of that terminology
is different in Python, then you'll just reinforce the O.P.'s
misunderstandings.
Python might have "references" in the generic sense, but in the specific
sense that it is understood by most people with C/Pascal/Java/Perl
experience, Python does not.
--
Steven
This is Python, not Perl. Please follow the links provided by Steven and
read carefully.
J. Peng writes:
But here, it is a global name rather than a parameter. However, I
don't speak Perl.
Tschö,
Torsten.
--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: bro...@jabber.org
(See http://ime.webhop.org for further contact info.)
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.cerut...@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
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.cerut...@gmail.com>
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.
> Sounds strange.
> In perl we can modify the variable's value like this way:
>
> $ perl -le '
>> $x=123;
>> sub test {
>> $x=456;
>> }
>> test;
>> print $x '
> 456
Not all that strange. The Python equivalent is
x=123
sub test()
global x
x=456
test()
print x
Python assignment works by binding an object with a name in a
namespace. I suspect that perl does something similar, and the
differences are in the rules about which namespace to use.
Mel.