美文网首页Python
Python的深度优先和广度优先

Python的深度优先和广度优先

作者: 老生住长亭 | 来源:发表于2018-03-30 21:05 被阅读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()

  1. 广度优先,代码例子

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()

相关文章

网友评论

本文标题:Python的深度优先和广度优先

本文链接:https://www.haomeiwen.com/subject/rocwcftx.html