A级和B级有什么区别?
自己怎么了?
为什么我需要逐行声明self?
class A(dict):
def __init__(self):
self={1:"you", 2:"and me"}
print "inside of class A",self
class B(dict):
def __init__(self):
self[1]="you"
self[2]="and me"
print "inside of class B",self
a=A()
print "outside of class A",a
b=B()
print "outside of class B",b
结果:
inside of class A {1: 'you', 2: 'and me'}
outside of class A {}
inside of class B {1: 'you', 2: 'and me'}
outside of class B {1: 'you', 2: 'and me'}