0

Python 有一个神奇的__getattr__方法,允许返回自定义值:

class A(object):
    def __getattr__(self, name):
        return name

B = A()
print B.foo  # 'foo'

但是,调用A.foo没有类似的效果,因为 A 不是实例。

使用 metaclasses,Google App Engine 会在实例化时引发此错误:

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__实现?

4

2 回答 2

2

元类解决方案应该可以工作,这里有一个例子:

class GetAttrMeta(type):
    def __getattr__(self, name):
        return name

class A(object):
    __metaclass__ = GetAttrMeta

print A.foo  # 'foo'

或者使用 Python 3.x:

class GetAttrMeta(type):
    def __getattr__(self, name):
        return name

class A(object, metaclass=GetAttrMeta):
    pass

print(A.foo)  # 'foo'
于 2012-08-04T00:02:11.607 回答
1

不确定这是否回答了您的问题,但也许结帐属性描述符..

class RevealAccess(object):
"""A data descriptor that sets and returns values
   normally and prints a message logging their access.
"""

def __init__(self, initval=None, name='var'):
    self.val = initval
    self.name = name

def __get__(self, obj, objtype):
    print 'Retrieving', self.name
    return self.val

def __set__(self, obj, val):
    print 'Updating' , self.name
    self.val = val

>>> class MyClass(object):
    x = RevealAccess(10, 'var "x"')
    y = 5
>>> MyClass.x
Retrieving var "x"
10
>>> MyClass().x
Retrieving var "x"
10
>>> 
>>> m = MyClass()
>>> m.x
Retrieving var "x"
10
>>> m.x = 20
Updating var "x"
>>> m.x
Retrieving var "x"
20
>>> m.y
5
于 2012-08-03T23:44:45.133 回答