Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()
like image 351
user2309239 Avatar asked Apr 22 '13 23:04

user2309239


2 Answers

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
like image 174
Felipe Avatar answered Nov 10 '22 13:11

Felipe


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.

like image 23
Sean Vieira Avatar answered Nov 10 '22 12:11

Sean Vieira