Attribute created in one method doesn't exist in other method

65,760

Solution 1

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

Solution 2

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.

Share:
65,760
user2309239
Author by

user2309239

Updated on July 09, 2022

Comments

  • user2309239
    user2309239 almost 2 years

    Here I have an attribute 'a', which is defined in first class method and should be changed in second. When calling them in order, this message appears:

    AttributeError: 'Class' object has no attribute 'a'

    The only way I've found - define 'a' again in second method, but in real code it has long inheritance and app will be messed. Why doesn't it work? Isn't self.a equal to Class.a?

    class Class(object):
        def method_1(self):
            self.a = 1
        def method_2(self):
            self.a += 1
    
    Class().method_1()
    Class().method_2()
    
  • abarnert
    abarnert about 11 years
    Great answer… but it's probably worth explaining that self.a is not equal to Class.a, instead of just flatly saying "no"). self.a is an instance attribute: each Class instance has its own copy. Class.a is a class attribute: the class itself has a single copy, no matter how many instances there are (sort of like a static member in C++ and related languages). And If you don't know why you'd want a class attribute, you don't want one.