Mutable default arguments don't generally do what you want. Instead, try this:
class Node:
def __init__(self, wordList=None, adjacencyList=None):
if wordList is None:
self.wordList = []
else:
self.wordList = wordList
if adjacencyList is None:
self.adjacencyList = []
else:
self.adjacencyList = adjacencyList
Answer from Michael J. Barber on Stack Overflowclass - Does Python have a default constructor for classes? - Stack Overflow
Default values in class definition and constructor? Is this the right way?
Don't use class attributes for that, that's not what they are there for. Use default arguments:
def __init__(self, name='Undefined', number=-1):
self.name = name
self.number = numberIf any of the arguments are mutable, then you need to use the usual idiom:
def __init__(self, foo=None):
self.foo = [] if foo is None else foo More on reddit.com what are constructors in python?
Explain Constructors in Python
Videos
Mutable default arguments don't generally do what you want. Instead, try this:
class Node:
def __init__(self, wordList=None, adjacencyList=None):
if wordList is None:
self.wordList = []
else:
self.wordList = wordList
if adjacencyList is None:
self.adjacencyList = []
else:
self.adjacencyList = adjacencyList
Let's illustrate what's happening here:
Python 3.1.2 (r312:79147, Sep 27 2010, 09:45:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo:
... def __init__(self, x=[]):
... x.append(1)
...
>>> Foo.__init__.__defaults__
([],)
>>> f = Foo()
>>> Foo.__init__.__defaults__
([1],)
>>> f2 = Foo()
>>> Foo.__init__.__defaults__
([1, 1],)
You can see that the default arguments are stored in a tuple which is an attribute of the function in question. This actually has nothing to do with the class in question and goes for any function. In python 2, the attribute will be func.func_defaults.
As other posters have pointed out, you probably want to use None as a sentinel value and give each instance it's own list.
I'm wondering if this is the right way to do this. It seems... odd somehow to assign default values in the class definition, then default None in the constructor. I suppose I could give the same default values in both places, but again, this feels like unnecessarily repeating myself.
class MyClass:
name = 'Undefined'
number = -1
def __init__(self, name=None, number=None):
if name is not None:
self.name = name
if number is not None:
self.number = numberDon't use class attributes for that, that's not what they are there for. Use default arguments:
def __init__(self, name='Undefined', number=-1):
self.name = name
self.number = number
If any of the arguments are mutable, then you need to use the usual idiom:
def __init__(self, foo=None):
self.foo = [] if foo is None else foo
do you need these vars on the class level, given that they are supposed to be relevant on a per instance basis?
class MyClass:
def __init__(self, name='Undefined', number=-1):
self.name = name
self.number = number
def __str__(self):
return '{0.__class__.__name__}(name={0.name}, number={0.number})'.format(self)
print(MyClass())
print(MyClass(name='Joe'))
print(MyClass(number=11))
print(MyClass(name='Joe', number=11))
its pretty confusing especially the ``def __init__`` one what does it exactly do? can anyone help me