我想让我的类(class)的用户能够定义一个转换函数,该函数在类(class)中多次应用。如果未定义此转换,则该值应保持不变。
这是我现在的(可能是冗长的)解决方案:
def do_nothing(val):
return val
class MyClass:
def __init__(self, transformation_function = None):
if transformation_function is None:
self.transform = do_nothing
else:
self.transform = transformation_function
我怎样才能把这个写得更简洁?
请您参考如下方法:
这个怎么样:
self.transform = transformation_function or lambda x: x
