Home > Software engineering >  How can I access to the attributes of an object of a class?
How can I access to the attributes of an object of a class?

Time:10-29

I have the following code:

class Animal:
    def __init__(self, age, name) -> None:
        self.age = age
        self.name = name
    
    def getAge(self):
        return self.age
    
    def getName(self):
        return self.name
    
class Animals:
    def __init__(self, index) -> None:
            self.index = index
            self.animalList = None
    
    def startInsert(self):
        newTable = list()
        for i in range(self.index):
            newTable.append(None)
        self.animalList = newTable
        
    def insertInIndex(self, index, animalObject):
        for i in range(len(self.animalList)):
            if i == index:
                self.animalList[i] = animalObject
    
    def show(this):
        for i in range(len(this.animalList)):
            if this.animalList[i] is not None:
                print('index: ',i,', Age: ',this.animalList[i].getAge,', Name: ',this.animalList[i].getAge)

animals = Animals(5)
animals.startInsert()
cow = Animal(2, "Cow")
dog = Animal(3, "Dog")
animals.insertInIndex(2, cow)
animals.insertInIndex(3, dog)
animals.show()

I want to send an object Animal to my list animalList which one is part of theAnimals class

My problem is that I can not access to the Age and Name attributes of my Animal class from my Animals class

My expected output is:

index: 2, Age: 2, Type: Cow
index: 3, Age: 3, Type: Dog

What I have done is:

def show(this):
        for i in range(len(this.animalList)):
            if this.animalList[i] is not None:
                print('index: ',i,', Age: ',this.animalList[i].getAge,', Name: ',this.animalList[i].getAge)

But my console just shows the next memory addresses:

index:  2 , Age:  <bound method Animal.getAge of <__main__.Animal object at 0x00000200ead03cd0>> , Type:  <bound method Animal.getAge of <__main__.Animal object at 0x00000200ead03cd0>>
index:  3 , Age:  <bound method Animal.getAge of <__main__.Animal object at 0x00000200ead03c70>> , Type:  <bound method Animal.getAge of <__main__.Animal object at 0x00000200ead03c70>>

Thanks.

CodePudding user response:

The problem is in this line:

print('index: ',i,', Age: ',this.animalList[i].getAge,', Name: ',this.animalList[i].getAge)

You are printing this.animalList[i].getAge, which is a function. To actually get the age, you must call the function like this.animalList[i].getAge().

This alone will resolve the question, however, your question has a few other (minor) issues. In the code below, I have removed your getter functions (which are unnecessary and are causing you confusion), corrected indentation, and refactored some code to make it look better. I also removed the animals.startInsert method, as it should be part of the constructor. I hope this helps you understand better:

class Animal:
    def __init__(self, age, name) -> None:
        self.age = age
        self.name = name


class Animals:
    def __init__(self, index) -> None:
        self.index = index
        self.animalList = [None for i in range(index)]

    def insertInIndex(self, index, animalObject):
        self.animalList[index] = animalObject

    def show(self):
        for i in range(len(self.animalList)):
            if self.animalList[i] is not None:
                print('index: ', i, ', Age: ', self.animalList[i].age,
                      ', Name: ', self.animalList[i].age)


animals = Animals(5)
cow = Animal(2, "Cow")
dog = Animal(3, "Dog")
animals.insertInIndex(2, cow)
animals.insertInIndex(3, dog)
animals.show()
  • Related