<p>这就是<a href="http://docs.python.org/howto/descriptor.html" rel="nofollow">descriptor protocol</a><code>property</code>基于的是:</p>
<pre><code>class Sasicorn(object):
def __init__(self, attr):
self.attr = "_" + attr
def __get__(self, obj, objtype):
return getattr(obj, self.attr) + ' sasquatch'
def __set__(self, obj, value):
setattr(obj, self.attr, value + ' unicorns')
class Foo(object):
def __init__(self, value = "bar"):
self.bar = value
self.baz = "baz"
bar = Sasicorn('bar')
baz = Sasicorn('baz')
foo = Foo()
foo2 = Foo('other')
print foo.bar
# prints bar unicorns sasquatch
print foo.baz
# prints baz unicorns sasquatch
print foo2.bar
# prints other unicorns sasquatch
</code></pre>
<p>虽然工厂函数中的<code>property</code>对于您的玩具示例来说可能还不错,但听起来您可能需要对实际用例进行更多的控制。在</p>