“元类分辨率顺序”中可能存在的错误? [英] Possible bug in "metaclass resolution order" ?

查看:45
本文介绍了“元类分辨率顺序”中可能存在的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述






我有一个A类,有元类M_A,B类是A的子类,有

元类M_B,M_A的子类。


一个C类,B的子类必须有M_B或它的子类作为元类,

但是如果我需要在C上禁用M_B中的代码吗?正确的方式

这样做似乎是M_C元类,M_B的子类,

实现但不调用父类方法,或调用''type''

方法。


但是如果我尝试使用与M_B无关的其他元类来做到这一点,我会得到一个

" TypeError:元类冲突异常正如预期的那样。


Python 2.4.1(#1,2005年9月16日,17:47:47)

[GCC 3.3.4] on linux2

输入help,copyright,credit等等。或许可证或更多信息。


Hi

I have a class A, with metaclass M_A, and class B, subclass of A, with
metaclass M_B, subclass of M_A.

A class C, subclass of B must have M_B or a subclass of it as metaclass,
but what if I need to ''disable'' the code in M_B on C ? The correct way
to do that seems to be with a M_C metaclass, subclass of M_B,
implementing but not calling parent class methods, or calling ''type''
methods.

But if I try to do that using other metaclass, not related to M_B, I get a
"TypeError: metaclass conflict exception" as expected.

Python 2.4.1 (#1, Sep 16 2005, 17:47:47)
[GCC 3.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.

class M_A(type):pass
.... class A:__ metaclass__ = M_A
....类M_B(M_A):传递
....类B(A):__ metaclass__ = M_B
....类M_C(类型):传递
...... C类(B):__ metaclass__ = M_C
....

回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:调用元类库时出错

元类冲突:派生类的元类必须是

(非严格)其所有基础元类的子类



问题是,如果我尝试用''type'做同样的事情'

口译员使用M_B而且我没有得到例外,警告或其他任何东西

其他。实际上,C类的__metaclass__属性指向

''类型''但是__class__到M_B!

类C(B):__ metaclass__ =类型
.... C .__ metaclass__
< type''type''> C .__ class__
< class''__ main __。M_B''> type(C)
class M_A(type): pass .... class A: __metaclass__ = M_A .... class M_B(M_A): pass .... class B(A): __metaclass__ = M_B .... class M_C(type): pass .... class C(B): __metaclass__ = M_C ....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a
(non-strict) subclass of the metaclasses of all its bases

The problem is, if I try to do the same thing with ''type'' the
interpreter use M_B and I don''t get an exception, warning or anything
else. In fact, the __metaclass__ attribute of the C class points to
''type'' but the __class__ to M_B!
class C(B): __metaclass__ = type .... C.__metaclass__ <type ''type''> C.__class__ <class ''__main__.M_B''> type(C)



< class''__ main __。M_B''>

由于显式__metaclass__属性有优先于父母

类,这样的情况是一个错误,应该引发一个例外,比如

元类冲突,对吧?


问候,


-

Pedro Werneck


<class ''__main__.M_B''>
Since the explicit __metaclass__ attribute has priority over parent
classes, a case like this is an error and should raise an exception like
the metaclass conflict, right ?

Regards,

--
Pedro Werneck

推荐答案

有你读过Metaclasses和Metaclasses。 在
Python 2.2中统一类型和类的一部分? ( http://www.python.org/2.2.3 /descrintro.html#metaclasses


它讨论并解释了您似乎遇到的问题。

Have you read the "Metaclasses" part of "Unifying types and classes in
Python 2.2"? (http://www.python.org/2.2.3/descrintro.html#metaclasses)

It discusses and explains the issues you seem to have.


2005年9月17日02:04:39 -0700

" Simon Percivall" < PE ******* @ gmail.com>写道:
On 17 Sep 2005 02:04:39 -0700
"Simon Percivall" <pe*******@gmail.com> wrote:
你读过Metaclasses吗? 在Python 2.2中统一类型和类的一部分? ( http://www.python.org/2.2.3 /descrintro.html#metaclasses
Have you read the "Metaclasses" part of "Unifying types and classes in
Python 2.2"? (http://www.python.org/2.2.3/descrintro.html#metaclasses)




是的,我读过。你读过并理解我的信息吗? :)


类A的子类,类A的子类,带有元类M_A应该有M_A或

它的子类作为元类。如果你尝试其他任何东西,你会得到一个

TypeError异常。好。但是如果你尝试使用''type'',就不会发生



Python 2.4.1(#1,2005年9月16日,17:47: 47)

[GCC 3.3.4] on linux2

输入help,copyright,credit等等。或许可证或更多信息。



Yes, I read. Have you read and understood my message ? :)

A class B, subclass of class A, with a metaclass M_A should have M_A or
a subclass of it as metaclass. If you try with anything else, you get a
TypeError exception as expected. OK. But if you try with ''type'', nothing
happens.

Python 2.4.1 (#1, Sep 16 2005, 17:47:47)
[GCC 3.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.

class M_A(type):pass
.... class A:__ metaclass__ = M_A
.... B类(A):__ metaclass__ =类型
.... B .__ class__
< class''__ main __。M_A''> B .__元类__
class M_A(type): pass .... class A: __metaclass__ = M_A .... class B(A): __metaclass__ = type .... B.__class__ <class ''__main__.M_A''> B.__metaclass__



< type''type''>

问候,

-

Pedro Werneck


<type ''type''>
Regards,

--
Pedro Werneck


我认为这更像是一个文档问题,而不是一个bug。

一开始可能看起来很奇怪,但是__metaclass__和__class__可能与
不同。


例如,如果M是元类
I think this is more of a documentation issue than of a bug.
It may seems strange at first, but __metaclass__ and __class__ may be
different.

For instance, if M is metaclass
class C(object):pass
C .__ metaclass__ = M
class C(object): pass C.__metaclass__ = M




你得到一个metaclass hook等于M的类,但C .__ class__是

仍然''type''。


在您的示例中,将__metaclass__设置为''type''不会更改

的元类

创建的类,它继承自基类。我建议您提交文件

bug。遗憾的是,有关元类的基本文档有点缺少b / b
,你可以通过试用和错误发现很多东西。


Michele Simionato



you get a class with metaclass hook equal to M, but C.__class__ is
still ''type''.

In you example, setting the __metaclass__ to ''type'' does not change the
metaclass of
the created class, which is inherited from the base class. I suggest
you to file a documentation
bug. Unfortunately the basic documentation about metaclasses is a bit
lacking and you have
to discover many things by trial and errors.

Michele Simionato


这篇关于“元类分辨率顺序”中可能存在的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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