如果我们有一个类有几个默认参数设置为 None,如果它们是 None,我们如何忽略它们,如果它们不是(或至少其中一个不是 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'