在我的一个类中,我有许多属性在获取和设置方面做了非常类似的事情。所以,我抽象的参数property
到工厂功能:继承Python的`属性`
def property_args(name):
def getter(self):
# do something
return getattr(self, '_' + name)
def setter(self, value)
# do something
setattr(self, '_' + name, value)
return getter, setter
class MyClass(object):
def __init__(self):
self._x = None
x = property(*property_args('x')) # obviously there's more than one of these IRL
但是,因为我已经发现property
实际上是一个类,继承它会为这个完美的。我在文档中找不到任何解释我需要重写的内容(以及__init__
等的参数签名),并且我并不想在C源代码中寻找它。有谁知道我在哪里可以找到这些信息?
作为参考,C实现可以在这里找到:http://hg.python.org/cpython/file/tip/Objects/descrobject.c – nneonneo
@nneonneo:谢谢!第1228行的大评论块就是答案。如果你想在完整的答案中写出这个,那么我会接受它。 –
poorsod你可以回答你自己的问题:) –