Python +;的操作数类型不受支持:';方法';和';浮动';

Python +;的操作数类型不受支持:';方法';和';浮动';,python,typeerror,Python,Typeerror,如何解决这个问题?谢谢coffee.price是一种方法,因此coffee.price+0.5会给出该错误 如果要获得该方法的结果,请调用该方法: class Coffee: def __init__(self): self._price=4.0 def price(self): return self._price def __str__(self): return "Coffee with price "+ str

如何解决这个问题?谢谢

coffee.price
是一种方法,因此
coffee.price+0.5
会给出该错误

如果要获得该方法的结果,请调用该方法:

class Coffee:
    def __init__(self):
        self._price=4.0

    def price(self):
        return self._price


    def __str__(self):
        return "Coffee with price "+ str(self._price)


class CoffeeWithMilk:
    def __init__(self, coffee):
        self.price+=coffee.price+0.5

    def price(self):
        return self.price


coffee=Coffee()

x=CoffeeWithMilk(coffee)
coffeeWithMilk=CoffeeWithMilk(x)
print(coffeeWithMilk)
注意,我将
+=
替换为
=
,在这里,您毕竟是在设置一个新属性。我还重命名了该属性,因为否则您的
CoffeeWithMilk.price
方法也会变得非常混乱,导致第二个错误,看起来几乎相同,因为
self.price
仍然是一个方法。这需要将
def price(self)
方法固定为:

self._price = coffee.price() + 0.5
因此,完成的代码如下所示:

def price(self):
    return self._price
通过使用类继承,可以避免重新定义
price
方法;用牛奶制作
咖啡
一种专门的
咖啡

class Coffee:
    def __init__(self):
        self._price = 4.0

    def price(self):
        return self._price

    def __str__(self):
        return "Coffee with price " + str(self._price)


class CoffeeWithMilk:
    def __init__(self, coffee):
        self._price = coffee.price() + 0.5

    def price(self):
        return self._price
您还将得到
\uuu str\uuuu
实现,因此最终的
打印(coffeeWithMilk)
将输出一些更有趣的内容

你也可以做
咖啡;属性是每次访问属性时自动为您调用的方法:

class Coffee:
    name = 'Coffee'

    def __init__(self):
        self._price = 4.0

    def price(self):
        return self._price

    def __str__(self):
        return "{} with price {}".format(self.name, self._price)


class CoffeeWithMilk(Coffee):
    name = 'Coffee with milk'

    def __init__(self, coffee):
        self._price = coffee.price() + 0.5
class Coffee:
    name = 'Coffee'

    def __init__(self):
        self._price = 4.0

    @property
    def price(self):
        return self._price

    def __str__(self):
        return "{} with price {}".format(self.name, self._price)


class CoffeeWithMilk(Coffee):
    name = 'Coffee with milk'

    def __init__(self, coffee):
        self._price = coffee.price + 0.5
在这种情况下,我不会使用方法或属性。无需将
\u price
隐藏在此处。只需将其替换为直接属性:

class Coffee:
    name = 'Coffee'

    def __init__(self):
        self._price = 4.0

    def price(self):
        return self._price

    def __str__(self):
        return "{} with price {}".format(self.name, self._price)


class CoffeeWithMilk(Coffee):
    name = 'Coffee with milk'

    def __init__(self, coffee):
        self._price = coffee.price() + 0.5
class Coffee:
    name = 'Coffee'

    def __init__(self):
        self._price = 4.0

    @property
    def price(self):
        return self._price

    def __str__(self):
        return "{} with price {}".format(self.name, self._price)


class CoffeeWithMilk(Coffee):
    name = 'Coffee with milk'

    def __init__(self, coffee):
        self._price = coffee.price + 0.5
这是因为方法和属性都只传递
\u price
属性。您也可以直接访问它

最后但并非最不重要的一点是,您从一个
CoffeeWithMilk
实例创建了一个
CoffeeWithMilk
实例,然后从第一个
CoffeeWithMilk
实例创建了另一个
CoffeeWithMilk
实例,因此您的最终实例添加了0.5到4两次:


为什么要进行就地添加?您还可以看到
price
是一种方法self.price=coffee.price()+0.5