ee94jrlc
ee94jrlc

Reputation: 1

How to fix Python error: AttributeError: type object X has no attribute Y

I'm creating a practice python file to better understand object oriented programming and I am getting the following error:

AttributeError: type object 'ID' has no attribute 'the_other_number'

I don't really understand why this happens because when I change the variable like below, it works just fine. I only receive an error when I try to use it in an if statement:

ID.the_other_number = 'new_value'

Below is my example code that I am trying to fix, any help is appreciated.

class ID():
   def __init__(self):
       self.other_number()
       pass

   def other_number(self):
       self.the_other_number = 3111

class ID_2():
    def __init__(self):
        self.update_number()

    def update_number(self):
       if ID.the_other_number > 4:
           print(ID.the_other_number)

if __name__ == '__main__':
   ID()
   ID_2()

I was expecting it to understand what variable is equal to and run the if statement correctly. Also, don't try to make sense of the code, I realize the code doesn't make sense, it is simply an example.

Upvotes: 0

Views: 6131

Answers (3)

Eshita Shukla
Eshita Shukla

Reputation: 811

You are making one mistake: You have to understand that ID is a class, it's not an object

So, if you want to use the function ID.the_other_number, you have to first create an object of ID. So, just add one extra line to your code

class ID():
   def __init__(self):
       self.other_number()
       pass

   def other_number(self):
       self.the_other_number = 3111

class ID_2():
    def __init__(self):
        self.update_number()

    def update_number(self):

        # Create one object of class ID - ID_object
        ID_object = ID()

        # Call the function for ID_object
        if ID_object.the_other_number > 4:
           print(ID_object.the_other_number)

if __name__ == '__main__':
   ID()
   ID_2()

Upvotes: 1

BoboDarph
BoboDarph

Reputation: 2891

@Patrick Haugh 's answer already explains why your code fails to execute the way you expect it. Absent the issues with indenting, here's how I would modify your code to print the_other_number property of class ID:

class ID():
    def __init__(self):
        self.other_number()

    def other_number(self):
        self.the_other_number = 3111
        return self.the_other_number

class ID_2():
    def __init__(self):
        self.update_number()

    def update_number(self):
        id = ID()
        if id.other_number() > 4:
            print(id.the_other_number)

if __name__ == '__main__':
   ID()
   ID_2()

Upvotes: 0

Patrick Haugh
Patrick Haugh

Reputation: 60944

When you run ID() a new instance of the ID class is created. __init__ calls the other_number method of that instance, which assigns the instance attribute the_other_number. Since you don't save that instance anywhere, by assigning the output of ID(), it's garbage collected immediately. The ID class does not change.

When you run ID_2(), you create an instance of the ID_2 class and run it's __init__ method. That calls the update_number method of that instance, which checks ID._the_other_number. ID has no the_other_number attribute, so an error is raised.

Read the Python tutorial on Classes, and pay special attention to the difference between class objects and instance objects.

Upvotes: 2

Related Questions