擅长:python、mysql、java
<p>我的一个同事建议使用闭包返回getter和setter函数,这也是我决定使用的。在</p>
<pre><code>class Foo(object):
def setter(var):
def set(self, value):
setattr(self, var, value+' unicorn')
return set
def getter(var):
def get(self):
return getattr(self, var)+' sasquatch'
return get
bar = property(getter('_bar'), setter('_bar'))
f = Foo()
f.foo = 'hi'
print f.foo
</code></pre>
<p>但感谢大家的回答:)</p>