擅长:python、mysql、java
<p>也许这会给你一些想法:</p>
<pre><code>class MyClass:
def get_status(self, status, other_param=None):
if other_param:
return self.data[status] + other_param
else:
return self.data[status]
def __init__(self):
self.data = {
'S': 1,
'someotherstatus': 2,
}
# without other params
setattr(self, 'someotherstatus', self.get_status('someotherstatus'))
# with an other param
setattr(self, 'someotherstatus_with_param', lambda other_param: self.get_status('someotherstatus', other_param))
obj = MyClass()
print(obj.someotherstatus) # 2
print(obj.someotherstatus_with_param(2)) # 4
</code></pre>