[英]Python Classes, not getting expected return values, confused with internal methods
我正在學習python,並且在Class Onject上苦苦掙扎,我有以下Class:
class Delivery(object):
def __init__(self, recipient, nn, cost, weight):
self.name = recipient
self.value = nn
self.cost = cost
self.weight = weight
def get_recipient(self):
return self.name
def get_priority_category(self):
if self.get_priority_value >= 8:
return "Urgent"
elif self.get_priority_value >= 5 and self.get_priority_value <= 7:
return "High"
elif self.get_priority_value >= 3 and self.get_priority_value <= 4:
return "Medium"
elif self.get_priority_value < 3:
return "Low"
def get_priority_value(self):
return self.nn
def get_cost(self):
return self.cost
def get_weight(self):
return self.weight
def get_cw_ratio(self):
ratio = self.cost / self.weight
return str(round(ratio, 2))
def __str__(self):
return '<' + self.get_recipient + ', ' + str(self.get_priority_category)+ ', ' + str(self.get_cost)+ ', ' + str(self.get_weight) + '>'
我希望發生的是:
PackageOne = Delivery('John', 1, 2, 4)
print(PackageOne)
結果應為<John, Low, 2, 4>
我怎么得到以下
<John, <bound method Delivery.get_priority_category of <__main__.Delivery object at 0x110866860>>, <bound method Delivery.get_cost of <__main__.Delivery object at 0x110866860>>, <bound method Delivery.get_weight of <__main__.Delivery object at 0x110866860>>>
我覺得我沒有在方法上使用正確的回報?
您沒有在調用您的方法。 將顯示方法對象本身的表示形式,而不是結果的表示形式。
添加()
調用:
def __str__(self):
return (
'<' + self.get_recipient() + ', ' +
self.get_priority_category() + ', ' +
str(self.get_cost()) + ', ' +
str(self.get_weight()) +
'>')
我刪除了多余的str()
調用( get_recipient()
和get_priority_category()
已經產生了字符串),並在表達式周圍添加了(...)
,以便可以將其分解成多行以提高可讀性。
並不是您需要大多數這些方法,因為您可以直接訪問基礎屬性:
def __str__(self):
return (
'<' + self.name + ', ' +
self.get_priority_category() + ', ' +
str(self.cost) + ', ' +
str(self.weight) +
'>')
在Python中,通常不使用訪問器函數,而直接訪問屬性就足夠了。 這與Java之類的語言不同,在Java之后,很難用訪問器函數替換屬性訪問。 在Python中,稍后切換到使用property
是微不足道的,因此直接使用屬性不會產生任何成本。
通過使用字符串格式可以簡化上述操作; 對於Python 3.6及更高版本,請使用f字符串:
def __str__(self):
return f'<{self.name}, {self.get_priority_category()}, {self.cost}, {self.weight}>'
否則,請使用str.format()
進行相同的操作:
def __str__(self):
return '<{}, {}, {}, {}>'.format(
self.name, self.get_priority_category(), self.cost, self.weight)
使用字符串格式時,不需要進行str()
調用,並且可以省去很多輸入'
和+
字符的麻煩。
您沒有調用所有方法,因此沒有打印出結果:
class Delivery(object):
def __init__(self, recipient, nn, cost, weight):
self.name = recipient
self.nn = nn
self.cost = cost
self.weight = weight
def get_recipient(self):
return self.name
def get_priority_category(self):
if self.get_priority_value() >= 8:
return "Urgent"
elif self.get_priority_value() >= 5 and self.get_priority_value() <= 7:
return "High"
elif self.get_priority_value() >= 3 and self.get_priority_value() <= 4:
return "Medium"
elif self.get_priority_value() < 3:
return "Low"
def get_priority_value(self):
return self.nn
def get_cost(self):
return self.cost
def get_weight(self):
return self.weight
def get_cw_ratio(self):
ratio = self.cost / self.weight
return str(round(ratio, 2))
def __str__(self):
return '<' + self.get_recipient() + ', ' + str(self.get_priority_category()) + ', ' + str(self.get_cost()) + ', ' + str(self.get_weight()) + '>'
PackageOne = Delivery('John', 1, 2, 4)
print(PackageOne)
返回值:
<John, Low, 2, 4>
必須通過add ()
調用方法,並且您還引用了nn
,必須將其更改為value
。
class Delivery(object):
def __init__(self, recipient, nn, cost, weight):
self.name = recipient
self.value = nn
self.cost = cost
self.weight = weight
def get_recipient(self):
return self.name
def get_priority_category(self):
if self.get_priority_value() >= 8:
return "Urgent"
elif self.get_priority_value() >= 5 and self.get_priority_value() <= 7:
return "High"
elif self.get_priority_value() >= 3 and self.get_priority_value() <= 4:
return "Medium"
elif self.get_priority_value() < 3:
return "Low"
def get_priority_value(self):
return self.value
def get_cost(self):
return self.cost
def get_weight(self):
return self.weight
def get_cw_ratio(self):
ratio = self.cost() / self.weight()
return str(round(ratio, 2))
def __str__(self):
return '<' + str(self.get_recipient()) + ', ' + str(self.get_priority_category())+ ', ' + str(self.get_cost())+ ', ' + str(self.get_weight()) + '>'
PackageOne = Delivery('John', 1, 2, 4)
print(PackageOne)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.