Python有一个神奇的__getattr__
方法,它允许将返回自定义值:实施Class.magic_class_method
class A(object):
def __getattr__(self, name):
return name
B = A()
print B.foo # 'foo'
但是,调用A.foo
有没有类似的效果,因为A不是一个实例。
Using metaclasses,谷歌应用程序引擎提出了在实例化这个错误:
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 913, in __init__
key_name.__class__.__name__)
BadKeyError: Name must be string type, not tuple
假设引用的问题是正确实施,有什么其他方法可以在魔法类__getattr__
实施?
这被试过(如引用的问题3155436),但Google的'db.Model'对象,我确实使用'class A (db.Model):',与元类扩展'type'冲突,导致上面显示的错误。 – Brian 2012-08-04 16:36:37
您应该尝试检查'db.Model'是否具有与类型不同的元类 - 并扩展它而不是'type'。 – jsbueno 2012-09-07 22:42:21