By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,046 Members | 2,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,046 IT Pros & Developers. It's quick & easy.

strange behaviour with keyword arguments and inheritance

P: n/a
Check out this toy example that demonstrates some "strange" behaviour
with keyword arguments and inheritance.

=================================

class Parent:
def __init__(self, ary = []):
self.ary = ary

def append(self):
self.ary.append(1)

class Child(Parent):
def __init__(self):
Parent.__init__(self)
self.append()

def main():
a = Child()
print a.ary
b = Child()
print b.ary

main()

=====================================

You would think the output of this program would be [1], [1]. But
strangely enough the output is [1,], [1,1]. I suppose that the
Parent.__class__ object is only created once and thus the keyword
argument always refers to the same thing, but I don't know. I have a
very rudimentary understading of python's guts, but I would still call
the behaviour unexpected. Or perhaps I should rtfm?

Any thoughts would be much appreciated. Thanks.

Apr 17 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Apr 17, 8:56 am, "matthewperpick" <matthewperp...@gmail.comwrote:
Check out this toy example that demonstrates some "strange" behaviour
with keyword arguments and inheritance.

=================================

class Parent:
def __init__(self, ary = []):
self.ary = ary

def append(self):
self.ary.append(1)

class Child(Parent):
def __init__(self):
Parent.__init__(self)
self.append()

def main():
a = Child()
print a.ary
b = Child()
print b.ary

main()

=====================================

You would think the output of this program would be [1], [1]. But
strangely enough the output is [1,], [1,1]. I suppose that the
Parent.__class__ object is only created once and thus the keyword
argument always refers to the same thing, but I don't know. I have a
very rudimentary understading of python's guts, but I would still call
the behaviour unexpected. Or perhaps I should rtfm?

Any thoughts would be much appreciated. Thanks.
A slight modification of init-ing the parent can give you the expected
output.

class Child(Parent):
def __init__(self):
Parent.__init__(self, [])
self.append()
Apr 17 '07 #2

P: n/a
On Apr 17, 8:56 am, "matthewperpick" <matthewperp...@gmail.comwrote:
Check out this toy example that demonstrates some "strange" behaviour
with keyword arguments and inheritance.

=================================

class Parent:
def __init__(self, ary = []):
self.ary = ary
This should work:

class Parent:
def __init__(self, ary = []):
self.ary = list(ary)

And FYI
http://groups.google.com/group/comp....89b250ceca1458

Apr 17 '07 #3

P: n/a
On Apr 17, 9:36 am, livibetter <livibet...@gmail.comwrote:
On Apr 17, 8:56 am, "matthewperpick" <matthewperp...@gmail.comwrote:
Check out this toy example that demonstrates some "strange" behaviour
with keyword arguments and inheritance.
=================================
class Parent:
def __init__(self, ary = []):
self.ary = ary

This should work:

class Parent:
def __init__(self, ary = []):
self.ary = list(ary)

And FYIhttp://groups.google.com/group/comp.lang.python/browse_thread/thread/...
livibetter has a better solution. the reason is that you need to
create a new list object everytime, am I right?

Apr 17 '07 #4

P: n/a
Ju**********************@gmail.com wrote:
On Apr 17, 9:36 am, livibetter <livibet...@gmail.comwrote:
>On Apr 17, 8:56 am, "matthewperpick" <matthewperp...@gmail.comwrote:
>>Check out this toy example that demonstrates some "strange" behaviour
with keyword arguments and inheritance.
=================================
class Parent:
def __init__(self, ary = []):
self.ary = ary
This should work:

class Parent:
def __init__(self, ary = []):
self.ary = list(ary)

And FYIhttp://groups.google.com/group/comp.lang.python/browse_thread/thread/...

livibetter has a better solution. the reason is that you need to
create a new list object everytime, am I right?
Yes, specifically on every *call*.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 17 '07 #5

P: n/a
Steve Holden <st***@holdenweb.comwrote:
...
livibetter has a better solution. the reason is that you need to
create a new list object everytime, am I right?
Yes, specifically on every *call*.
....and livibetter's solution also creates a new list on every call to
Child (that [] passed on to Parent.__init__ does exactly that).
Alex
Apr 17 '07 #6

P: n/a
matthewperpick a écrit :
Check out this toy example that demonstrates some "strange" behaviour
with keyword arguments and inheritance.
Nope. It demonstrates that default arguments are eval'd only once (when
the def statement is eval'd), which is documented and a FAQ.
I have a
very rudimentary understading of python's guts, but I would still call
the behaviour unexpected. Or perhaps I should rtfm?
Well, since you mention it... !-)

Apr 17 '07 #7

P: n/a

matthewperpick wrote:
Check out this toy example that demonstrates some "strange" behaviour
with keyword arguments and inheritance.

=================================

class Parent:
def __init__(self, ary = []):
self.ary = ary
[snip]

As pointed out earlier, default values for arguments are evaluated
when the function is defined, not when it is called. This creates
confusion if this value is mutable and later mutated; I got confused
by it when I started python. So it is often not a good idea to use
mutable objects as default arguments.

A simple fix:

def __init__(self, ary=None):
if ary is None: ary = []
self.ary = ary

--
Arnaud

Apr 17 '07 #8

P: n/a
cool .. thanks everyone. here is the aforementioned faq.

http://www.python.org/doc/faq/genera...etween-objects

On Apr 17, 5:16 am, Arnaud Delobelle <arno...@googlemail.comwrote:
matthewperpick wrote:
Check out this toy example that demonstrates some "strange" behaviour
with keyword arguments and inheritance.
=================================
class Parent:
def __init__(self, ary = []):
self.ary = ary

[snip]

As pointed out earlier, default values for arguments are evaluated
when the function is defined, not when it is called. This creates
confusion if this value is mutable and later mutated; I got confused
by it when I started python. So it is often not a good idea to use
mutable objects as default arguments.

A simple fix:

def __init__(self, ary=None):
if ary is None: ary = []
self.ary = ary

--
Arnaud

Apr 17 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.