Python类,没有得到预期的返回值,与内部方法混淆

2024-04-26 20:21:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习python,我正在努力学习这个类,我有下面的类:

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>>>

我觉得我用的方法不对?你知道吗


Tags: selfgetreturnobjectvaluedefnndelivery
3条回答

方法必须通过添加()来调用,您还引用了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)

你不能调用你的方法。您将看到方法对象本身的表示,而不是它们的结果。你知道吗

添加()调用:

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等语言不同,后者很难在事后用访问器函数替换属性访问。在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>

相关问题 更多 >