Python获取_str__方法错误
我正在从一本Python编程书中学习OOP,其中一个例子是关于使用Python获取_str__方法错误,python,oop,constructor,Python,Oop,Constructor,我正在从一本Python编程书中学习OOP,其中一个例子是关于使用\uu str\uuu()函数通过print()语句显示属性值。这本书不清楚,我想我遗漏了一些重要的东西: class Product: def __init__(self, description, price, inventory): self.__description = description self.__price = price self.__invent
\uu str\uuu()
函数通过print()
语句显示属性值。这本书不清楚,我想我遗漏了一些重要的东西:
class Product:
def __init__(self, description, price, inventory):
self.__description = description
self.__price = price
self.__inventory = inventory
def __str__(self):
return '{0} - price: {1:.2f}, inventory: {2:d}'.format(self.__description(), self.__price(), self.__inventory())
def get_description(self):
return self.__description
def get_price(self):
return self.__price
def get_inventory(self):
return self.__inventory
当我运行模块,创建一个对象,并使用print()
函数时,我得到以下错误,即“str”对象不可调用:
>prod1=产品(“番茄”,1.50,20)
>>>打印(prod1)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
打印(prod1)
文件“C:/Users/person/Documents/GitHub/pyprojects/inittest.py”,第8行,在__
返回'{0}-价格:{1:.2f},库存:{2:d}'。格式(self.\u description(),self.\u price(),self.\u inventory())
TypeError:“str”对象不可调用
>>>
我应该如何处理
\uu str\uu()
函数?谢谢。您正在尝试调用字符串
def __str__(self):
return '{0} - price: {1:.2f}, inventory: {2:d}'.format(self.__description(), self.__price(), self.__inventory())
您需要删除“()”:
或者使用getter方法
def __str__(self):
return '{0} - price: {1:.2f}, inventory: {2:d}'.format(self.get__description(), self.get__price(), self.get__inventory())
您正在尝试调用字符串
def __str__(self):
return '{0} - price: {1:.2f}, inventory: {2:d}'.format(self.__description(), self.__price(), self.__inventory())
您需要删除“()”:
或者使用getter方法
def __str__(self):
return '{0} - price: {1:.2f}, inventory: {2:d}'.format(self.get__description(), self.get__price(), self.get__inventory())
您正在尝试调用字符串
def __str__(self):
return '{0} - price: {1:.2f}, inventory: {2:d}'.format(self.__description(), self.__price(), self.__inventory())
您需要删除“()”:
或者使用getter方法
def __str__(self):
return '{0} - price: {1:.2f}, inventory: {2:d}'.format(self.get__description(), self.get__price(), self.get__inventory())
您正在尝试调用字符串
def __str__(self):
return '{0} - price: {1:.2f}, inventory: {2:d}'.format(self.__description(), self.__price(), self.__inventory())
您需要删除“()”:
或者使用getter方法
def __str__(self):
return '{0} - price: {1:.2f}, inventory: {2:d}'.format(self.get__description(), self.get__price(), self.get__inventory())
您使用的是
self.\uu description()
,但您真正的意思是self.\uu description
,或self.get\u description()
。self.\uu price()
和self.\uu inventory()
也是如此。不要在标识符的开头使用\uu
。这将调用通常做您不想做的事情,并且不能替代Java/C++中的private
scoping,不要编写琐碎的“getter”。它们是来自C++/Java的反模式。最好使用my\u product.inventory
而不是my\u product.get\u inventory()
。与其他语言不同,如果您以后需要添加方法来维护成员不变量,您可以这样做,而无需更改提及*.inventory的每个位置。您使用的是self.\uu description()
,但您真正指的是self.\uu description
或self.get\u description()
。self.\uu price()
和self.\uu inventory()
也是如此。不要在标识符的开头使用\uu
。这将调用通常做您不想做的事情,并且不能替代Java/C++中的private
scoping,不要编写琐碎的“getter”。它们是来自C++/Java的反模式。最好使用my\u product.inventory
而不是my\u product.get\u inventory()
。与其他语言不同,如果您以后需要添加方法来维护成员不变量,您可以这样做,而无需更改提及*.inventory的每个位置。您使用的是self.\uu description()
,但您真正指的是self.\uu description
或self.get\u description()
。self.\uu price()
和self.\uu inventory()
也是如此。不要在标识符的开头使用\uu
。这将调用通常做您不想做的事情,并且不能替代Java/C++中的private
scoping,不要编写琐碎的“getter”。它们是来自C++/Java的反模式。最好使用my\u product.inventory
而不是my\u product.get\u inventory()
。与其他语言不同,如果您以后需要添加方法来维护成员不变量,您可以这样做,而无需更改提及*.inventory的每个位置。您使用的是self.\uu description()
,但您真正指的是self.\uu description
或self.get\u description()
。self.\uu price()
和self.\uu inventory()
也是如此。不要在标识符的开头使用\uu
。这将调用通常做您不想做的事情,并且不能替代Java/C++中的private
scoping,不要编写琐碎的“getter”。它们是来自C++/Java的反模式。最好使用my\u product.inventory
而不是my\u product.get\u inventory()
。与其他语言不同的是,如果以后需要添加方法来维护成员不变量,则无需更改提及*.inventory的每个位置。