strange behaviour with keyword arguments and inheritance

M

matthewperpick

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.
 
J

Jun.Jin.act+group.python

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()
 
J

Jun.Jin.act+group.python

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?
 
S

Steve Holden

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
 
A

Alex Martelli

Steve Holden said:
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
 
B

Bruno Desthuilliers

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... !-)
 
A

Arnaud Delobelle

matthewperpick said:
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
 
M

matthewperpick

cool .. thanks everyone. here is the aforementioned faq.

http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects

matthewperpick said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top