class test:
def __init__(self,val=None):
self.val = [] if val is None else val
I think "None" is the only default value that should be used for methods. When subclassing I kept running into problems with default arguments hiding that no value had been provided. In the example below I wanted b to "inherit" a's default value.
class a:
def __init__(self, value=2):
self.value = value
def __repr__(self):
return str(self.value)
class b(a):
def __init__(self, value=None): # Want superclass to provide default
super().__init__(value)
print(b(), b(5))
Output:
None 5
The inheritance works if I use None in the method signature and set the value in the method body.
class a:
def __init__(self, value=None):
self.value = 2 if value is None else value
def __repr__(self):
return str(self.value)
class b(a):
def __init__(self, value=None): # Want superclass to provide default
super().__init__(value)
print(b(), b(5))
Output:
2 5