Tay
11-11-2012, 08:43 PM
I have a question about class variables and scope in Python.
Here is the code:
######
class Super:
def __init__(self):
a = "this is b, the b of Super without self"
self.b="this is b, the self.b of Super"
class Sub(Super):
def __init__(self):
Super.__init__(self)
c = "this is c, of sub, without self"
self.d = "this is d, self.d of Sub"
abcd = Sub()
######
Why wouldn't I be able to type abcd.a and get the value of a? I must be misunderstanding something--isn't the point of the __init__ function in the superclass to initialize variables in the subclasses, so that they can be used without having to define them in every class using self?
Here is the code:
######
class Super:
def __init__(self):
a = "this is b, the b of Super without self"
self.b="this is b, the self.b of Super"
class Sub(Super):
def __init__(self):
Super.__init__(self)
c = "this is c, of sub, without self"
self.d = "this is d, self.d of Sub"
abcd = Sub()
######
Why wouldn't I be able to type abcd.a and get the value of a? I must be misunderstanding something--isn't the point of the __init__ function in the superclass to initialize variables in the subclasses, so that they can be used without having to define them in every class using self?