我正在写一个应该除其他类之外的类的类code.InteractiveInterpreter
。由于某种原因,compile
当您使用多重继承时,该类通常具有()的方法之一在其子类上不可用。
单继承工作正常:
>>> from code import InteractiveInterpreter
>>> class Single(InteractiveInterpreter): pass
...
>>> hasattr(Single(), 'compile')
True
多重继承不:
>>> class Double(object, InteractiveInterpreter): pass
...
>>> hasattr(Double(), 'compile')
False
翻转订单,但可以:
>>> class Flipped(InteractiveInterpreter, object): pass
...
>>> hasattr(Flipped(), 'compile')
True
我是否不知道多重继承的一些细微细节compile
在某些情况下会阻止继承,或者是Python中的错误导致了这种情况(考虑到方法的名称也是内置方法的名称,在功能上,我觉得这是可能的。
我正在尝试使用其他类来重现此问题,InteractiveInterpreter
但无法执行以下操作:
>>> class Original():
... def compile(self): pass
...
>>> class Secondary(object, Original): pass
...
>>> hasattr(Secondary(), 'compile')
True
我在Windows 10上使用32位Python 2.7.11。
您确定Flipped
产生了给定的结果吗?
我得到了类似的设置,
>>> from code import InteractiveInterpreter
>>> class Flipped(InteractiveInterpreter, object): pass
...
>>> hasattr(Flipped(), 'compile')
True
基于对模块的源,compile
是不是方法的类的,但一个实例属性上对象初始化创建。有道理的是,从object
first继承不会提供属性,因为它是__init__
已定义的,并且子类不会调用InteractiveInterpreter.__init__
以分配属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句