我如何重写类的__getattr__方法而不破坏默认行为?
当前回答
重写__getattr__应该没问题——__getattr__只会在最后的情况下调用,即在实例中没有匹配该名称的属性时调用。例如,如果你访问foo。Bar,那么__getattr__只会在foo没有名为Bar的属性时被调用。如果该属性是你不想处理的,则引发AttributeError:
class Foo(object):
def __getattr__(self, name):
if some_predicate(name):
# ...
else:
# Default behaviour
raise AttributeError
然而,与__getattr__不同,__getattribute__将首先被调用(仅适用于新样式类,即继承自object的类)。在这种情况下,你可以像这样保留默认行为:
class Foo(object):
def __getattribute__(self, name):
if some_predicate(name):
# ...
else:
# Default behaviour
return object.__getattribute__(self, name)
请参阅Python文档了解更多信息。
其他回答
重写__getattr__应该没问题——__getattr__只会在最后的情况下调用,即在实例中没有匹配该名称的属性时调用。例如,如果你访问foo。Bar,那么__getattr__只会在foo没有名为Bar的属性时被调用。如果该属性是你不想处理的,则引发AttributeError:
class Foo(object):
def __getattr__(self, name):
if some_predicate(name):
# ...
else:
# Default behaviour
raise AttributeError
然而,与__getattr__不同,__getattribute__将首先被调用(仅适用于新样式类,即继承自object的类)。在这种情况下,你可以像这样保留默认行为:
class Foo(object):
def __getattribute__(self, name):
if some_predicate(name):
# ...
else:
# Default behaviour
return object.__getattribute__(self, name)
请参阅Python文档了解更多信息。
class A(object):
def __init__(self):
self.a = 42
def __getattr__(self, attr):
if attr in ["b", "c"]:
return 42
raise AttributeError("%r object has no attribute %r" %
(self.__class__.__name__, attr))
>>> a = A()
>>> a.a
42
>>> a.b
42
>>> a.missing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in __getattr__
AttributeError: 'A' object has no attribute 'missing'
>>> hasattr(a, "b")
True
>>> hasattr(a, "missing")
False
为了扩展Michael answer,如果你想使用__getattr__保持默认行为,你可以这样做:
class Foo(object):
def __getattr__(self, name):
if name == 'something':
return 42
# Default behaviour
return self.__getattribute__(name)
现在异常消息更具描述性:
>>> foo.something
42
>>> foo.error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in __getattr__
AttributeError: 'Foo' object has no attribute 'error'
推荐文章
- 使用元组/列表等等。从输入vs直接引用类型如list/tuple/etc
- 结合conda环境。Yml和PIP requirements.txt
- 将命名元组转换为字典
- 如何使x轴和y轴的刻度相等呢?
- Numpy在这里函数多个条件
- 在Python中,使用argparse只允许正整数
- 如何排序mongodb与pymongo
- 不可变与可变类型
- 列表是线程安全的吗?
- 操作系统。makdirs在我的路径上不理解“~”
- 如何在Django模板中获得我的网站的域名?
- 在django Forms中定义css类
- 如何在Python中scp ?
- Numpy Max vs amax vs maximum
- 我应该在.gitignore文件中添加Django迁移文件吗?