If we have a class with a few default arguments set to None, how do we ignore them if they are None, and use them if they are not (or at least one of them is not None)?
class Foo:
def __init__(self, first=1, second=2, third=3, fourth=None, fifth=None):
self.first = first
self.second = second
self.third = third
self.fourth = fourth
self.fifth = fifth
self.sum = self.first + self.second + self.third + self.fourth + self.fifth
return self.sum
>>> c = Foo()
Traceback (most recent call last):
File "<pyshell#120>", line 1, in <module>
c = Foo()
File "<pyshell#119>", line 8, in __init__
self.sum = self.first + self.second + self.third + self.fourth + self.fifth
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
Then it will add zero with no side effects instead of
NoneYou could also change the part where you add them up and test for
Nonefirst but this is probably less typing.