What is the difference between class A and class B?
What’s wrong with self?
Why do I need to declare self line by line?
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
result:
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'}
This doesn’t modify the object passed as
self, but re-binds the local variableselfto a new dict.