Python的深度优先和广度优先
2018-03-30 本文已影响3人
老生住长亭
1.深度优先,直接上例子,可以运行代码
class P1:
def foo(self):
print 'called P1-foo';
def bar(self):
print 'called P1-bar';
class P2:
def foo(self):
print 'called P2-foo';
def bar(self):
print 'called P2-bar';
class C1(P1, P2):
pass;
class C2(P1, P2):
def bar(self):
print 'called C2-bar()';
class GC(C1, C2):
pass;
gc = GC();
gc.foo();
gc.bar();
结果:
called P1-foo
called P1-bar()
- 广度优先,代码例子
class P1(object):
def foo(self):
print 'called P1-foo';
def bar(self):
print 'called P1-bar';
class P2(object):
def foo(self):
print 'called P2-foo';
def bar(self):
print 'called P2-bar';
class C1(P1, P2):
pass;
class C2(P1, P2):
def bar(self):
print 'called C2-bar()';
class GC(C1, C2):
pass;
gc = GC();
gc.foo();
gc.bar();
结果:
called P1-foo
called C2-bar()