为什么我不能在这里访问 .__mro__ 属性?
Why can't I access .__mro__ attribute here?
示例来自 Raymond Hettinger's recepie
class Root(object):
def draw(self):
# the delegation chain stops here
assert not hasattr(super(Root, self), 'draw')
class Shape(Root):
def __init__(self, shapename, **kwds):
self.shapename = shapename
super(Shape, self).__init__(**kwds)
def draw(self):
print 'Drawing. Setting shape to:', self.shapename
super(Shape, self).draw()
class ColoredShape(Shape):
def __init__(self, color, **kwds):
self.color = color
super(ColoredShape, self).__init__(**kwds)
def draw(self):
print 'Drawing. Setting color to:', self.color
super(ColoredShape, self).draw()
# ------- Show how to incorporate a non-cooperative class --------
class Moveable(object):
# non-cooperative class that doesn't use super()
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self):
print 'Drawing at position:', self.x, self.y
class MoveableAdapter(Root):
# make a cooperative adapter class for Moveable
def __init__(self, x, y, **kwds):
self.moveable = Moveable(x, y)
super(MoveableAdapter, self).__init__(**kwds)
def draw(self):
self.moveable.draw()
super(MoveableAdapter, self).draw()
class MovableColoredShape(ColoredShape, MoveableAdapter):
pass
m = MovableColoredShape(color='red', shapename='triangle', x=10, y=20)
m.draw()
print m.__mro__
我理解这段代码。我不明白的是为什么我在执行这段代码时会收到这个错误:
Traceback (most recent call last):
File "study_mro.py", line 47, in <module>
print m.__mro__
AttributeError: 'MovableColoredShape' object has no attribute '__mro__'
出于神秘原因,__mro__
属性仅在 class 上可见,而不是在实例上可见。
试试这个:
>>> m.__class__.__mro__
(<class '__main__.MovableColoredShape'>, <class '__main__.ColoredShape'>, <class '__main__.Shape'>, <class '__main__.MoveableAdapter'>, <class '__main__.Root'>, <type 'object'>)
示例来自 Raymond Hettinger's recepie
class Root(object):
def draw(self):
# the delegation chain stops here
assert not hasattr(super(Root, self), 'draw')
class Shape(Root):
def __init__(self, shapename, **kwds):
self.shapename = shapename
super(Shape, self).__init__(**kwds)
def draw(self):
print 'Drawing. Setting shape to:', self.shapename
super(Shape, self).draw()
class ColoredShape(Shape):
def __init__(self, color, **kwds):
self.color = color
super(ColoredShape, self).__init__(**kwds)
def draw(self):
print 'Drawing. Setting color to:', self.color
super(ColoredShape, self).draw()
# ------- Show how to incorporate a non-cooperative class --------
class Moveable(object):
# non-cooperative class that doesn't use super()
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self):
print 'Drawing at position:', self.x, self.y
class MoveableAdapter(Root):
# make a cooperative adapter class for Moveable
def __init__(self, x, y, **kwds):
self.moveable = Moveable(x, y)
super(MoveableAdapter, self).__init__(**kwds)
def draw(self):
self.moveable.draw()
super(MoveableAdapter, self).draw()
class MovableColoredShape(ColoredShape, MoveableAdapter):
pass
m = MovableColoredShape(color='red', shapename='triangle', x=10, y=20)
m.draw()
print m.__mro__
我理解这段代码。我不明白的是为什么我在执行这段代码时会收到这个错误:
Traceback (most recent call last):
File "study_mro.py", line 47, in <module>
print m.__mro__
AttributeError: 'MovableColoredShape' object has no attribute '__mro__'
出于神秘原因,__mro__
属性仅在 class 上可见,而不是在实例上可见。
试试这个:
>>> m.__class__.__mro__
(<class '__main__.MovableColoredShape'>, <class '__main__.ColoredShape'>, <class '__main__.Shape'>, <class '__main__.MoveableAdapter'>, <class '__main__.Root'>, <type 'object'>)