繁体   English   中英

Python类(未获得预期的返回值)与内部方法混淆

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM