There is much more to it than you think. Consider the defaults to be static (=constant reference pointing to one object) and stored somewhere in the definition; evaluated at method definition time; as part of the class, not the instance. As they are constant, they cannot depend on self
.
Here is an example. It is counterintuitive, but actually makes perfect sense:
def add(item, s=[]):
s.append(item)
print len(s)
add(1) # 1
add(1) # 2
add(1, []) # 1
add(1, []) # 1
add(1) # 3
This will print 1 2 1 1 3
.
Because it works the same way as
default_s=[]
def add(item, s=default_s):
s.append(item)
Obviously, if you modify default_s
, it retains these modifications.
There are various workarounds, including
def add(item, s=None):
if not s: s = []
s.append(item)
or you could do this:
def add(self, item, s=None):
if not s: s = self.makeDefaultS()
s.append(item)
Then the method makeDefaultS
will have access to self
.
Another variation:
import types
def add(item, s=lambda self:[]):
if isinstance(s, types.FunctionType): s = s("example")
s.append(item)
here the default value of s
is a factory function.
You can combine all these techniques:
class Foo:
import types
def add(self, item, s=Foo.defaultFactory):
if isinstance(s, types.FunctionType): s = s(self)
s.append(item)
def defaultFactory(self):
""" Can be overridden in a subclass, too!"""
return []
self.someattribute
in default argument value. – Hodeidaself
doesn't exist until runtime. – Margartmargate