我有这样一个初始值设定项:
def __init__(self, options=None, rinse=True, algorithm="foo"):
if options is not None:
self.options = options
else:
self.create_default()
if (rinse==False): #would mean rinse parameter was passed
self.options.rinse = rinse
if (algorithm!="foo"): #would mean algorithm parameter was passed
self.options.algorithm = algorithm
我想能够调用以上几种方式。以下是一些例子:
a = MyClass() #nothing was passed
a = MyClass(rinse: False) #only rinse parameter was passed
a = MyClass(algorithm: "bar") #only algorithm parameter was passed
a = MyClass(options, rinse, algorithm) #pass all three arguments
问题
如何构造这个初始值设定项,以便传递选择性参数。你知道吗
如果您愿意将示例改为使用等号而不是冒号,那么
__init__
应该可以正常工作:相关问题 更多 >
编程相关推荐