class - How to Create a Printing Method in Python -


i'm new python , have little background in c++. i'm getting mind around object oriented programming, having problem defining print methods in class created. have read both repr() , str() functions, neither of them seems work.

class chicken():     def __init__(self, name, eatable, species):         self.name=name         if eatable!="yes" , eatable!="no":             self.eatable="no"         else:             self.eatable=eatable         self.species=species     #print species when called     #doesnt work reason     def print_species(self):         print "%s" %str(self.species)     #print whether or not bird eatable     #this method doesnt work     def print_eatable(self):         print "%s" %str(self.eatable)     def change_species(self, new_name):         self.species=new_name     def change_eatable(self, new_status):         self.eatable=new_status         if new_status!="yes" , new_status!="no":             self.eatable="no"         else:             self.eatable=new_status peep=chicken("peep", "yes", "gallus domesticus") peep.change_eatable("meatballs") peep.print_eatable peep.print_species #this work print peep.eatable 

can explain how can create method can print information object (ie. peep.print_species="gallus domesticus")?

thanks

you need add parentheses @ end of function calls:

peep.print_eatable() peep.print_species() 

so code should be:

class chicken():     def __init__(self, name, eatable, species):         self.name=name         if eatable!="yes" , eatable!="no":             self.eatable="no"         else:             self.eatable=eatable         self.species=species     #print species when called     #doesnt work reason     def print_species(self):         print"%s" %str(self.species)     #print whether or not bird eatable     #this method doesnt work     def print_eatable(self):         print "%s" %str(self.eatable)     def change_species(self, new_name):         self.species=new_name     def change_eatable(self, new_status):         self.eatable=new_status         if new_status!="yes" , new_status!="no":             self.eatable="no"         else:             self.eatable=new_status peep=chicken("peep", "yes", "gallus domesticus") peep.change_eatable("meatballs") peep.print_eatable() peep.print_species() #this work print peep.eatable 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -