正如标题所示。看起来不管我做什么,__getattr__
都不会被调用。我也尝试过(荒谬的,我知道),可预测的没有回应。好像__getattr__
在元类中被禁止。元类的__getattr__未被调用
我很感激任何指向文档的指针。
代码:
class PreinsertMeta(type):
def resolvedField(self):
if isinstance(self.field, basestring):
tbl, fld = self.field.split(".")
self.field = (tbl, fld)
return self.field
Field = property(resolvedField)
def __getattr__(self, attrname):
if attrname == "field":
if isinstance(self.field, basestring):
tbl, fld = self.field.split(".")
self.field = (tbl, fld)
return self.field
else:
return super(PreinsertMeta, self).__getattr__(attrname)
def __setattr__(self, attrname, value):
super(PreinsertMeta, self).__setattr__(attrname, value)
class Test(object):
__metaclass__ = PreinsertMeta
field = "test.field"
print Test.field # Should already print the tuple
Test.field = "another.field" # __setattr__ gets called nicely
print Test.field # Again with the string?
print Test.Field # note the capital 'F', this actually calls resolvedField() and prints the tuple
感谢BrenBarn,这里的最后工作实现:
class PreinsertMeta(type):
def __getattribute__(self, attrname):
if attrname == "field" and isinstance(object.__getattribute__(self, attrname), basestring):
tbl, fld = object.__getattribute__(self, attrname).split(".")
self.field = (tbl, fld)
return object.__getattribute__(self, attrname)
谢谢,忘了'__getattribute__'以及:)关于它的一切意义:上面的实现是针对父类的子类,其中'field'成员不能直接引用基类成员,因为它没有'当时存在。 __getattribute__版本允许我在使用时使用已解析的成员变量。既然封闭和子类都没有实例化,我尝试用meta类来实现。 – velis