Default list parameter issue

  • Thread starter Olivier Boudeville
  • Start date
O

Olivier Boudeville

Hi all,

it must be a very stupid question but I cannot find out the explanation
for my code's behaviour, so any help would really be appreciated.


My purpose was, simply put, to have a tree-like data structure, thanks
to the following (simplified to track the issue) Node class :

class Node:


def __init__( self, newContent = None ) :
"""Creates an empty node with no child node."""
self.content = newContent
self.children = []

def __repr__( self ):
"""Returns a textual representation of this node's state."""
res = "Node has "
if self.content:
res += "content (%s)" % ( self.content, )
else:
res += "no content"
res += " and it has "
if self.children:
res += "%s child(ren) : <%s>" % ( len( self.children ),self.children )
else:
res += "no child."

return res

def addChild( self, aChild ):
"""Adds a child to current node."""
self.children.append( aChild )



I test the class simply thanks to :
a=Node()
b=Node()
print a
print b
a.addChild( b )
print a
print b

and it returns, as expected :
Node has no content and it has 1 child(ren) : <[Node has no content and
it has no child.]>Node has no content and it has no child.

That is ok. But if I swap the previous __init__ method with my first
version :

def __init__( self, newContent = None, newChildren = [] ) :
"""Creates an empty node with no child node."""
self.content = newContent
self.children = newChildren

and I apply the same test, I got (output not edited) :
Node has no content and it has 1 child(ren) : <[Node has no content and
Node has no content and it has 1 child(ren) : <[Node has no content and

I do not understand why using an empty list as default parameter results
in such a different behaviour.

Thanks in advance for any hint,
kind regards,

Olivier.


PS : tested with python 2.2.1 and 2.3.3. The test should be relevant since :
> diff ok.py ko.py
2,4c2,4
<
<
< def __init__( self, newContent = None ) :
---
>
>
> def __init__( self, newContent = None, newChildren = [] ) :
7c7
< self.children = []
---
> self.children = newChildren

Olivier.
 
O

Olivier Boudeville

Oh, thanks a lot Peter, I did not know about it and was totally stuck.

IMHO, this 'feature' is counter-intuitive. Maybe it allows to write in
some cases simplier or more efficient functions, but there are enough
traps not to add more of them, with this kind of side-effects.

I believed, with filter, list comprehension and so on, that python was
making some steps towards 'functional programming' and declarative
languages (sorry if the translations are not accurate). It would have
led to more robust code. It is certainly not that way that recursive
functions will be easy to write and understand (but it is just my
opinion) !

I do not find/wish this behaviour is 'pythonic', but of course it is a
matter of taste.

Thanks again nevertheless, it helped me a lot !

Olivier.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top