I am unsure how to correctly get the attribute from a list of instances. Sorry if this seems simple, I am only a beginner.
class Clown:
def __init__(self,name,hours,job):
self.name = name
self.hours = hours
self.job = job
def get_job(self):
return self.job
def change_job(self, new_job):
self.job = new_job
list_of_clowns = [Clown("Tom",3,"puppets"), Clown("Jeff",1,"ballon animals")]
clown1 = Clown("Andy",4,"unicycle")
print(list_of_clowns[0].get_job) #problem
print(clown1.get_job()) #this works and prints "unicycle"
When I use
print(list_of_clowns[0].get_job)
it gives
<bound method Clown.get_job of <__main__.Clown object at 0x000001CE928DE20>>
when I want
puppets
CodePudding user response:
By printing list_of_clowns[0].get_job
you're printing the definition of the method def get_job(self):
.
Changing it to list_of_clowns[0].get_job()
(note the parentheses), you will execute the method that than will return puppets
.