简体   繁体   中英

Python Classes, not getting expected return values, confused with internal methods

I am learning python and I am struggling with the Class Onject, I have the below 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) + '>'

What I expect to happen is this:

PackageOne = Delivery('John', 1, 2, 4)
print(PackageOne)

result should be <John, Low, 2, 4>

how ever I get the following

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

I feel like I'm not using the right return on the methods?

You are not calling your methods. You are shown the representation of the method objects themselves, not their results.

Add () calls:

def __str__(self):        
    return (
        '<' + self.get_recipient() + ', ' +
              self.get_priority_category() + ', ' + 
              str(self.get_cost()) + ', ' +
              str(self.get_weight()) + 
        '>')

I dropped redundant str() calls ( get_recipient() and get_priority_category() are already producing strings), and added (...) around the expression so it can be broken up across multiple lines for readability.

Not that you need most of those methods, because you could just access the underlying attributes directly:

def __str__(self):        
    return (
        '<' + self.name + ', ' +
              self.get_priority_category() + ', ' + 
              str(self.cost) + ', ' +
              str(self.weight) + 
        '>')

In Python, you generally don't use accessor functions, not when accessing the attribute directly would suffice. This differs from languages such as Java, where it is hard to replace attribute access with an accessor function after the fact. In Python, it is trivial to switch to using a property later on so there is no cost with just using attributes directly.

The above can be simplified by using string formatting; for Python 3.6 and up, use an f-string:

def __str__(self):        
    return f'<{self.name}, {self.get_priority_category()}, {self.cost}, {self.weight}>'

otherwise use str.format() to do the same:

def __str__(self):        
    return '<{}, {}, {}, {}>'.format(
        self.name, self.get_priority_category(), self.cost, self.weight)

When using string formatting, no str() calls are necessary and you save your self a lot of typing out ' and + characters.

You did not call all your methods, therefore no results had been printed out:

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)

Returns:

<John, Low, 2, 4>

Methods must be called via adding () , and also you referred to nn , which must be changed to 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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