Alex
Alex

Reputation: 85

getting bound method error when calling from class in python

Here is the code for the program:

import petspgm

def main():
    pets = make_list()
    print('Here is the data you entered: ')
    display_lists(pets)

def make_list():
    pet_list = []

print('Enter the data for the 3 pets')
for count in range(1,4):
    # get the pet data
    print('Pet number' + str(count) + ':')
    pet_name = input("Enter your pet's name: ")
    pet_type = input("Enter the type of pet: ")
    pet_age = input("Enter your pet's age: ")
    print

    pet = petspgm.PetData(pet_name, pet_type, pet_age)

    pet_list.append(pet)

return pet_list

def display_lists(pet_list):
    for item in pet_list:
        print("Pet's name is: " + item.get_pet_name())
        print("Pet's type is: " + item.get_pet_type())
        print("Pet's age is: " + str (item.get_pet_age))
main()

Here is the code for the petspgm.py PetData class:

class PetData:

# The __init__ method initializes the attributes
def __init__(self, pet_name, pet_type, pet_age):
    self.__pet_name = pet_name
    self.__pet_type = pet_type
    self.__pet_age = pet_age

# This method accepts an argument for the pet's name    
def set_pet_name(self, pet_name):
    self.__pet_name = pet_name

# This method acepts an argument for the pet's type
def set_pet_type(self, pet_type):
    self.__pet_type = pet_type

# This method accepts an argument for the pet's age
def set_pet_age(self, pet_age):
    self.__pet_age = pet_age

# This method returns the pet's name
def get_pet_name(self):
    return self.__pet_name

# This method returns the pet's type
def get_pet_type(self):
    return self.__pet_type

# This method returns the pet's age
def get_pet_age(self):
    return self.__pet_age

Here is the error I am getting, it is not returning the age.. please help:

Pet's name is: sam

Pet's type is: dog

Pet's age is: bound method PetData.get_pet_age of petspgm.PetData object at 0x02D1E6D0

Pet's name is: rand

Pet's type is: dog

Pet's age is: bound method PetData.get_pet_age of petspgm.PetData object at 0x02D1E730

Pet's name is: sprinkles

Pet's type is: cat

Pet's age is: bound method PetData.get_pet_age of petspgm.PetData object at 0x02D1E770

Upvotes: 0

Views: 4015

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117876

You are missing the () to actually call the method

print("Pet's age is: " + str (item.get_pet_age))

Should be

print("Pet's age is: " + str (item.get_pet_age()))

Upvotes: 3

Related Questions