bac*_*aco 2 python dictionary class self
A级和B级有什么区别?
自我有什么问题?
为什么我需要逐行声明自己?
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
Run Code Online (Sandbox Code Playgroud)
结果:
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'}
Run Code Online (Sandbox Code Playgroud)
def __init__(self):
self={1:"you", 2:"and me"}
Run Code Online (Sandbox Code Playgroud)
这不会修改传递的对象self,但会将局部变量重新绑定self到新的dict.