如何更新Python中class属性的值?

How to update the value of class attrubutes in Python?

So I have this simple code for a class CoffeeMachine. The question is: Is there a way to simplify def buy so that instead of subtracting values from each parameter, the value of all parameters will be updated at once? something like

return CoffeeMachine(- 250, - 0, - 16, - 1, + 4)

而不是:

self.water -= 250
self.beans -= 16
self.money += 4
self.cups -= 1

代码如下(删除了多余的函数,这样会更容易阅读)

class CoffeeMachine:

    def __init__(self, water, milk, beans, cups, money):
        self.water = water
        self.milk = milk
        self.beans = beans
        self.cups = cups
        self.money = money

    def buy(self):
        coffee_type = input('What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:')
        if coffee_type == '1':
            if self.water <= 250:
                print('Sorry, not enough water!')
            elif self.beans <= 16:
                print('Sorry, not enough beans!')
            else:
                print('I have enough resources, making you a coffee!')
                self.water -= 250
                self.beans -= 16
                self.money += 4
                self.cups -= 1
        elif coffee_type == '2':
            if self.water <= 350:
                print('Sorry, not enough water!')
            elif self.milk <= 75:
                print('Sorry, not enough milk!')
            elif self.beans <= 20:
                print('Sorry, not enough beans!')
            else:
                self.water -= 350
                self.milk -= 75
                self.beans -= 20
                self.money += 7
                self.cups -= 1
                print('I have enough resources, making you a coffee!')
        elif coffee_type == '3':
            if self.water <= 200:
                print('Sorry, not enough water!')
            elif self.milk <= 100:
                print('Sorry, not enough milk!')
            elif self.beans <= 12:
                print('Sorry, not enough beans!')
            else:
                self.water -= 200
                self.milk -= 100
                self.beans -= 12
                self.money += 6
                self.cups -= 1
                print('I have enough resources, making you a coffee!')
        elif coffee_type == 'back':
            pass

Coffee_Machine = CoffeeMachine(400, 540, 120, 9, 550)
Coffee_Machine.power_on()

您可以将 buy 方法逻辑拆分为尝试购买特定咖啡类型的不同方法。然后创建一个更新值的新方法(假设值足够):

def buy(self):
    coffee_type = input('What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:')
    if coffee_type == '1':
        self.attempt_to_buy_espresso()
    # ...

def attempt_to_buy_espresso(self):
    price = 4
    required_water = 250
    required_beans = 16
    if self.water <= required_water:
        print('Sorry, not enough water!')
    elif self.beans <= required_beans:
        print('Sorry, not enough beans!')
    else:
        print('I have enough resources, making you a coffee!')
    self.prepare_drink(price=price, water=required_water, beans=required_beans)

def prepare_drink(self, price, water=0, beans=0, milk=0)
    self.money += price
    self.water -= water
    self.beans -= beans
    self.milk -= milk
    self.cups -= 1

您可以进一步改进,例如通过将每种饮料所需的水、豆类和牛奶值存储在统一的数据结构(例如字典)中,这样它们就不会分布在不同的方法中,或者将准备饮料和更新货币值的逻辑分开,因为那些代表单独的动作。

Class属性是class中的变量,其值等于class的所有objects/instances。我们可以通过 class name 来改变 class 属性的值。 让我们看看下面的 class x

class x:
    a=7
    def __init__(self,z):
         self.b=z
    def sum(self):
         return(self.a+self.b)
e=x(3)
f=x(4)
print("Initial sum\n")
print(e.sum(),f.sum())
print("\n")
x.a=5
print("Class attribute has been changed\n")
print("After changing class attribute\n")
print(e.sum(),f.sum())

输出:

Initial sum

10 11
Class attribute has been changed

After changing class attribute

8 9