function parameter scope python 2.5.2

J

J Kenneth King

I recently encountered some interesting behaviour that looks like a bug
to me, but I can't find the appropriate reference to any specifications
to clarify whether it is a bug.

Here's the example code to demonstrate the issue:

class SomeObject(object):

def __init__(self):
self.words = ['one', 'two', 'three', 'four', 'five']

def main(self):
recursive_func(self.words)
print self.words

def recursive_func(words):
if len(words) > 0:
word = words.pop()
print "Popped: %s" % word
recursive_func(words)
else:
print "Done"

if __name__ == '__main__':
weird_obj = SomeObject()
weird_obj.main()


The output is:

Popped: five
Popped: four
Popped: three
Popped: two
Popped: one
Done
[]

Of course I expected that recursive_func() would receive a copy of
weird_obj.words but it appears to happily modify the object.

Of course a work around is to explicitly create a copy of the object
property befor passing it to recursive_func, but if it's used more than
once inside various parts of the class that could get messy.

Any thoughts? Am I crazy and this is supposed to be the way python works?
 
J

J Kenneth King

J Kenneth King said:
I recently encountered some interesting behaviour that looks like a bug
to me, but I can't find the appropriate reference to any specifications
to clarify whether it is a bug.

Here's the example code to demonstrate the issue:

class SomeObject(object):

def __init__(self):
self.words = ['one', 'two', 'three', 'four', 'five']

def main(self):
recursive_func(self.words)
print self.words

def recursive_func(words):
if len(words) > 0:
word = words.pop()
print "Popped: %s" % word
recursive_func(words)
else:
print "Done"

if __name__ == '__main__':
weird_obj = SomeObject()
weird_obj.main()


The output is:

Popped: five
Popped: four
Popped: three
Popped: two
Popped: one
Done
[]

Of course I expected that recursive_func() would receive a copy of
weird_obj.words but it appears to happily modify the object.

Of course a work around is to explicitly create a copy of the object
property befor passing it to recursive_func, but if it's used more than
once inside various parts of the class that could get messy.

Any thoughts? Am I crazy and this is supposed to be the way python works?

Of course, providing a shallow (or deep as necessary) copy makes it
work, I'm curious as to why the value passed as a parameter to a
function outside the class is passed a reference rather than a copy.
 
G

George Sakkis

I recently encountered some interesting behaviour that looks like a bug
to me, but I can't find the appropriate reference to any specifications
to clarify whether it is a bug.
Here's the example code to demonstrate the issue:
class SomeObject(object):
    def __init__(self):
        self.words = ['one', 'two', 'three', 'four', 'five']
    def main(self):
        recursive_func(self.words)
        print self.words
def recursive_func(words):
    if len(words) > 0:
        word = words.pop()
        print "Popped: %s" % word
        recursive_func(words)
    else:
        print "Done"
if __name__ == '__main__':
    weird_obj = SomeObject()
    weird_obj.main()
The output is:
Popped: five
Popped: four
Popped: three
Popped: two
Popped: one
Done
[]
Of course I expected that recursive_func() would receive a copy of
weird_obj.words but it appears to happily modify the object.
Of course a work around is to explicitly create a copy of the object
property befor passing it to recursive_func, but if it's used more than
once inside various parts of the class that could get messy.
Any thoughts? Am I crazy and this is supposed to be the way python works?

Of course, providing a shallow (or deep as necessary) copy makes it
work, I'm curious as to why the value passed as a parameter to a
function outside the class is passed a reference rather than a copy.

Why should it be a copy by default ? In Python all copies have to be
explicit.

George
 
R

Rafe

I recently encountered some interesting behaviour that looks like a bug
to me, but I can't find the appropriate reference to any specifications
to clarify whether it is a bug.

Here's the example code to demonstrate the issue:

class SomeObject(object):

    def __init__(self):
        self.words = ['one', 'two', 'three', 'four', 'five']

    def main(self):
        recursive_func(self.words)
        print self.words

def recursive_func(words):
    if len(words) > 0:
        word = words.pop()
        print "Popped: %s" % word
        recursive_func(words)
    else:
        print "Done"

if __name__ == '__main__':
    weird_obj = SomeObject()
    weird_obj.main()

The output is:

Popped: five
Popped: four
Popped: three
Popped: two
Popped: one
Done
[]

Of course I expected that recursive_func() would receive a copy of
weird_obj.words but it appears to happily modify the object.

Of course a work around is to explicitly create a copy of the object
property befor passing it to recursive_func, but if it's used more than
once inside various parts of the class that could get messy.

Any thoughts? Am I crazy and this is supposed to be the way python works?

You are passing a mutable object. So it can be changed. If you want a
copy, use slice:
L = [1, 2, 3, 4, 5]
copy = L[:]
L.pop() 5
L [1, 2, 3, 4]
copy
[1, 2, 3, 4, 5]

....in your code...

def main(self):
recursive_func(self.words[:])
print self.words

....or...
def recursive_func(words):
words = words[:]
if len(words) > 0:
word = words.pop()
print "Popped: %s" % word
recursive_func(words)
else:
print "Done"

words = ["one", "two", "three"]
recursive_func(words)
Popped: three
Popped: two
Popped: one
Done['one', 'two', 'three']

Though I haven't been doing this long enough to know if that last
example has any drawbacks.

If we knew more about what you are trying to do, perhaps an
alternative would be even better.

- Rafe
 
S

Steven D'Aprano

Of course I expected that recursive_func() would receive a copy of
weird_obj.words but it appears to happily modify the object.

I am curious why you thought that. What made you think Python should/did
make a copy of weird_obj.words when you pass it to a function?

This is a serious question, I'm not trying to trap you into something :)
 
A

Arnaud Delobelle

J Kenneth King said:
I recently encountered some interesting behaviour that looks like a bug
to me, but I can't find the appropriate reference to any specifications
to clarify whether it is a bug.

Here's the example code to demonstrate the issue:

class SomeObject(object):

def __init__(self):
self.words = ['one', 'two', 'three', 'four', 'five']

def main(self):
recursive_func(self.words)
print self.words

def recursive_func(words):
if len(words) > 0:
word = words.pop()
print "Popped: %s" % word
recursive_func(words)
else:
print "Done"

if __name__ == '__main__':
weird_obj = SomeObject()
weird_obj.main()


The output is:

Popped: five
Popped: four
Popped: three
Popped: two
Popped: one
Done
[]

Of course I expected that recursive_func() would receive a copy of
weird_obj.words but it appears to happily modify the object.

Of course a work around is to explicitly create a copy of the object
property befor passing it to recursive_func, but if it's used more than
once inside various parts of the class that could get messy.

Any thoughts? Am I crazy and this is supposed to be the way python works?

That's because Python isn't call-by-value. Or it is according to some,
it's just that the values it passes are references. Which, according to
others, is unnecessarily convoluted: it's call-by-object, or shall we
call it call-by-sharing? At least everybody agrees it's not
call-by-reference or call-by-name.

There. I hope this helps!
 
J

J Kenneth King

alex23 said:
You're passing neither a reference nor a copy, you're passing the
object (in this case a list) directly:

http://effbot.org/zone/call-by-object.htm

Ah, thanks -- that's precisely what I was looking for.

I knew it couldn't be a mistake; I just couldn't find the documentation
on the behaviour since I didn't know what it was called in the python
world.

Cheers.
 
J

J Kenneth King

Steven D'Aprano said:
I am curious why you thought that. What made you think Python should/did
make a copy of weird_obj.words when you pass it to a function?

This is a serious question, I'm not trying to trap you into something :)

Don't worry, I don't feel "trapped" in usenet. ;)

It was more of an intuitive expectation than a suggestion that Python
got something wrong.

I was working on a program of some complexity recently and quickly
caught the issue in my tests. I knew what was going on and fixed it
expediently, but the behaviour confused me and I couldn't find any
technical documentation on it so I figured I just didn't know what it
was referred to in Python. Hence the post. :)

I suppose I have some functional sensibilities and assumed that an
object wouldn't let a non-member modify its properties even if they were
mutable.

Of course if there is any further reading on the subject, I'd appreciate
some links.

Cheers.
 
P

Peter Pearson

Steven D'Aprano said:
I am curious why you thought that. What made you think Python should/did
make a copy of weird_obj.words when you pass it to a function?
[snip]
Of course if there is any further reading on the subject, I'd appreciate
some links.

As one relatively new Python fan to another, I recommend following
this newsgroup. Many important aspects of Python that several books
failed to drive through my skull are very clearly (and repeatedly)
explained here. Hang around for a week, paying attention to posts
with subjects like "Error in Python subscripts" (made-up example),
and curse me if you don't find it greatly rewarding.
 
J

J Kenneth King

Peter Pearson said:
Steven D'Aprano said:
I am curious why you thought that. What made you think Python should/did
make a copy of weird_obj.words when you pass it to a function?
[snip]
Of course if there is any further reading on the subject, I'd appreciate
some links.

As one relatively new Python fan to another, I recommend following
this newsgroup. Many important aspects of Python that several books
failed to drive through my skull are very clearly (and repeatedly)
explained here. Hang around for a week, paying attention to posts
with subjects like "Error in Python subscripts" (made-up example),
and curse me if you don't find it greatly rewarding.

I do lurk more often than I post and sometimes I help out people new to
Python or new to programming in general. I know how helpful usenet can
be and usually this group in particular is quite special. It's good
advice to read before you post; quite often the question has been
proposed and answered long before it came to your little head (not you
in particular; just general "you").

In this case, I was simply lacking the terminology to find what I was
looking for on the subject. In such cases turning to the community seems
like a fairly reasonable way to find clarification. I've only been
programming in Python specifically for two years or so now, so I hope I
can be forgiven.

Cheers.
 
T

Terry Reedy

J said:
I was working on a program of some complexity recently and quickly
caught the issue in my tests. I knew what was going on and fixed it
expediently, but the behaviour confused me and I couldn't find any
technical documentation on it so I figured I just didn't know what it
was referred to in Python. Hence the post. :)

Language Reference / Expressions / Primaries / Calls +
Language Reference / Compound statements / Function definitions

Hmm. Read by themselves, these are not as clear as they could be that
what parameters get bound to are the argument objects. One really needs
to have read the section on assignment statements first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top