Attribute created in one method doesn't exist in other method
Short answer, no. The problem with your code is that each time you create a new instance.
Edit: As abarnert mentions below, there is a big difference between Class.a
and c.a
. Instance attributes (the second case) belong to each specific object, whereas class attributes belong to the class. Look at abarnert's comment below or the discussion here for more info.
Your code is equivalent to
c1 = Class()
c1.method_1() # defines c1.a (an instance attribute)
c2 = Class()
c2.method_2() # c2.a undefined (the c2 instance doesn't have the attribute)
You probably want to do somthing like
c = Class()
c.method_1() # c.a = 1
c.method_2() # c.a = 2
print "c.a is %d" % c.a # prints "c.a is 2"
Or probably even better would be to initialize c
with an a
attribute
class Class:
def __init__(self):
self.a = 1 # all instances will have their own a attribute
A newly-created instance of Class
has no attribute a
when you do instance_of_class.method_2()
without calling method_1
, as in your example.
Consider this slightly altered version of your code:
class CreateNewClassInstance(object):
def create_a(self):
self.a = 1
def add_one_to_a(self):
self.a += 1
CreateNewClassInstance().create_a()
CreateNewClassInstance().add_one_to_a()
Each time you call Class()
(or CreateNewClassInstance()
) you create a new object, with its own attribute a
. Until you initialize a
, you don't have an attribute with that name.
Most of the time this isn't an issue - however, +=
will attempt to load self.a
before adding one to it - which is what is causing your AttributeError
in this case.