如何在Python中创建打印方法 我对Python相当陌生,并且有一些C++背景。我刚刚开始考虑面向对象编程,但在我创建的这个类中定义print方法时遇到了一个问题。我已经阅读了repr()和str()函数,但它们似乎都不起作用 class chicken(): def __init__(self, name, eatable, species): self.name=name if eatable!="yes" and eatable!="no": self.eatable="no" else: self.eatable=eatable self.species=species #Print the species when called #Doesnt work for some reason def print_species(self): print "%s" %str(self.species) #Print whether or not the bird is eatable #This method also 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" and 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 does work print peep.eatable
有谁能解释一下我如何创建一个方法,这样我就可以简单地打印有关对象的信息(即peep.print_species=“Gallus Domesticus”)如何在Python中创建打印方法 我对Python相当陌生,并且有一些C++背景。我刚刚开始考虑面向对象编程,但在我创建的这个类中定义print方法时遇到了一个问题。我已经阅读了repr()和str()函数,但它们似乎都不起作用 class chicken(): def __init__(self, name, eatable, species): self.name=name if eatable!="yes" and eatable!="no": self.eatable="no" else: self.eatable=eatable self.species=species #Print the species when called #Doesnt work for some reason def print_species(self): print "%s" %str(self.species) #Print whether or not the bird is eatable #This method also 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" and 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 does work print peep.eatable,python,class,object,printing,Python,Class,Object,Printing,有谁能解释一下我如何创建一个方法,这样我就可以简单地打印有关对象的信息(即peep.print_species=“Gallus Domesticus”) 谢谢函数调用末尾缺少括号 peep.print_eatable() peep.print_species() 当您在没有括号的情况下编写peep.print_species时,Python不会报告语法错误,因为它是一个完全合法的语句,返回方法对象print_species,您需要调用方法,而不仅仅是获取它们。将()放在语句后面: peep=c
谢谢函数调用末尾缺少括号
peep.print_eatable()
peep.print_species()
当您在没有括号的情况下编写peep.print_species时,Python不会报告语法错误,因为它是一个完全合法的语句,返回方法对象print_species
,您需要调用方法,而不仅仅是获取它们。将()
放在语句后面:
peep=chicken("Peep", "yes", "Gallus Domesticus")
peep.change_eatable("meatballs")
peep.print_eatable()
您需要在函数调用的末尾添加括号:
peep.print_eatable()
peep.print_species()
所以代码应该是:
class chicken():
def __init__(self, name, eatable, species):
self.name=name
if eatable!="yes" and eatable!="no":
self.eatable="no"
else:
self.eatable=eatable
self.species=species
#Print the species when called
#Doesnt work for some reason
def print_species(self):
print"%s" %str(self.species)
#Print whether or not the bird is eatable
#This method also 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" and 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 does work
print peep.eatable
函数调用后需要括号,一点也不需要。我们都去过那里!