使用场景:
# case #1 - for classes
a = MyClass() # default logger is None
a = MyClass(logger="a") # set the default logger to be "a"
a.test(logger="b") # this means that logger will be "b" only inside this method
a.test(logger=None) # this means that logger will be None but only inside this method
a.test() # here logger should defaults to the value specified when object was initialized ("a")
MyClass
为了能够像上面那样使用它,我该如何实现?
假设我有几个MyClass
可以接受logger
命名参数的方法,所以我希望有一个不需要在每个test...()
方法的开头添加大量重复代码的解决方案。
我阅读了哨兵示例,但这不适用于类,我不想添加一个全局变量来将哨兵对象保留在里面。