__getattr__,newstyle和oldstyle类的不对称行为 [英] Asymmetric behavior for __getattr__, newstyle vs oldstyle classes

查看:117
本文介绍了__getattr__,newstyle和oldstyle类的不对称行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次写这里,对不起,如果消息是没有文档或太长。

this is the first time I write here, sorry if the message is unfocuessed or too long.

我有兴趣了解更多关于如何在需要时获取对象的属性。因此,我阅读了名为数据模型的Python 2.7文档这里,我遇到了 __ getattr __ ,为了检查我是否理解它的行为,我写了这些简单的(和不完整的)字符串封装。

I was interested in understanding more about how objects'attributes are fetched when needed. So I read the Python 2.7 documentation titled "Data Model" here, I met __getattr__ and, in order to check whether I understood or not its behavior, I wrote these simple (and incomplete) string wrappers.

class OldStr:
  def __init__(self,val):
    self.field=val

  def __getattr__(self,name):
    print "method __getattr__, attribute requested "+name

class NewStr(object):
  def __init__(self,val):  
    self.field=val

  def __getattr__(self,name):
    print "method __getattr__, attribute requested "+name

正如你所看到的,除了是oldstyle和newstyle类之外,它们是一样的。因为引用的文本说 __ getattr __ 是当属性查找没有找到属性在通常的地方时调用,我想尝试一个+操作这些类的两个实例

As you can see they are the same except for being an oldstyle vs newstyle classes. Since the quoted text says __getattr__ is "Called when an attribute lookup has not found the attribute in the usual places", I wanted to try a + operation on two instances of those classes to see what happened, expecting an identical behavior.

但结果让我有些困惑:

>>> x=OldStr("test")
>>> x+x
method __getattr__, attribute requested __coerce__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

我没有定义一个方法 __ coerce __ (虽然我期待一个请求 __ add __ ,nevermind :),因此 __ getattr __ 参与并返回了一个无用的东西。但

Good! I did not define a method for __coerce__ (although I was expecting a request for __add__, nevermind :), so __getattr__ got involved and returned a useless thing. But then

>>> y=NewStr("test")
>>> y+y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NewStr' and 'NewStr'

为什么在使用内置运算符(如+)时,oldstyle类中的 __ getattr __ 和newstyle类之间的不对称行为?有人可以帮我了解发生了什么,以及我在阅读文档时的错误是什么?

Why this asymmetric behavior between __getattr__ in oldstyle classes and newstyle ones when using a built-in operator like +? Could someone please help me to understand what is going on, and what was my mistake in reading the documentation?

非常感谢您的帮助!

推荐答案

请参阅新方法类的特殊方法查找

特殊方法直接在类对象,实例对象中查找,因此 __ getattr __() __ getattribute __()被绕过。出于同样的原因, instance .__ add__ = foobar 不起作用。

Special methods are directly looked up in the class object, the instance objects and consequently __getattr__() and __getattribute__() are bypassed. For precisely the same reason, instance.__add__ = foobar doesn't work.

属性访问。特殊方法被非常频繁地调用,并且使它们受标准的,相当复杂的属性查找会显着地减慢解释器。

This is done to speed up attribute access. Special methods are called very frequently, and making them subject to the standard, rather complex attribute lookup would significantly slow down the interpreter.

旧式类的属性查找很多较不复杂(最着名的旧式类不支持描述符),因此没有理由在旧样式类中处理不同的特殊方法。

Attribute lookup for old-style classes is much less complex (most notably old style classes do not support descriptors), so there is no reason to treat special methods differently in old style classes.

这篇关于__getattr__,newstyle和oldstyle类的不对称行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆