#!/usr/bin/python2
class A(object):
print ‘in A class…’
def __init__(self):
print ‘-‘ * 20
print ‘init ‘, self.__class__.__name__,’ calss…’
print ‘Created Class:’, self.__class__.__name__
print ‘out ‘, self.__class__.__name__,’A calss…’
print ‘-‘ * 20
print ‘out A class…’
class B(A):
print ‘in B class…’
class C(B):
print ‘in B class…’
class D(B, A):
print ‘in D class…’
a = A()
b = B()
c = C()
d = D()
outputs:
in A class…
out A class…
in B class…
in B class…
in D class…
--------------------
init A calss…
Created Class: A
out A A calss…
--------------------
--------------------
init B calss…
Created Class: B
out B A calss…
--------------------
--------------------
init C calss…
Created Class: C
out C A calss…
--------------------
由输出可见,当class在声明的时候其中的语句就可被执行,而其中的init只会在实例化时被调用。