This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: euc-jp -*-" | |
class Base1: | |
pass | |
class Base2: | |
def method(self): | |
print('Base2') | |
class MyClass(Base1, Base2): | |
pass | |
here = MyClass() | |
here.method() # Base2 | |
class BaseBase: | |
def method(self): | |
print('BaseBase') | |
class Base1(BaseBase): | |
pass | |
class Base2(BaseBase): | |
def method(self): | |
print('Base2') | |
class MyClass(Base1, Base2): | |
pass | |
# 古いMROは左から右への深さ優先というルールに従って動作するため、 | |
# Base2を探す前に、最初のBase1クラスの親を探索できるところまで | |
# 探索しようとする。故に下のようなコードを書くとBaseBaseまでいきつく | |
here = MyClass() | |
here.method() # BaseBase | |
class BaseBase(object): | |
def method(self): | |
print('BaseBase') | |
class Base1(BaseBase): | |
pass | |
class Base2(BaseBase): | |
def method(self): | |
print('Base2') | |
class MyClass(Base1, Base2): | |
pass | |
here = MyClass() | |
here.method() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment