如何使用我自己 class 在 python 中定义的变量正确打印句子
how do I properly print a sentence using variables defined in my own class in python
所以我在 python 中创建了一个名为 Pizza 的 class,它有 4 个参数...我希望能够插入参数然后打印它,以便它以一个干净的句子例如,如果我将其放入终端---
from Pizza import Pizza
appetizer = Pizza("Pepperoni", 16, 10.50, 10)
print(appetizer)
我想得到这个结果---
您的意大利辣香肠比萨饼直径为 16 英寸,价格
10.5 美元,每个馅饼 10 片
不幸的是,在我的代码中,当我打印出变量时,我一直得到这个---
<Pizza.Pizza object at 0x7f17b762f650>
有人知道为什么会这样吗?我的代码如下
class Pizza:
def __init__(self, name, diameter, price, slices):
self.name = name
self.diameter = diameter
self.price = price
self.slices = slices
def myfunc(self):
print("Your" + self.name + "pizza has a diameter of" + self.diameter + "inches, a price of" + self.price + "and" + self.slices + "slices per pie")
尝试
from Pizza import Pizza
appetizer = Pizza("Pepperoni", 16, 10.50, 10).myfunc()
#you don't need to print appetizer because myfunc does it already
另外,将 class 披萨改成这个
class Pizza:
def __init__(self, name, diameter, price, slices):
self.name = name
self.diameter = diameter
self.price = price
self.slices = slices
def myfunc(self):
print("Your " + self.name + " pizza has a diameter of " + str(self.diameter) + " inches, a price of " + str(self.price) + " and " + str(self.slices) + " slices per pie")
所以它不会导致“类型错误:只能将 str(不是“int”)连接到 str”错误
您告诉 Python 打印 Pizza 对象,所以它很乐意遵从 :)。同样在您的 myfunc()
方法中,您将字符串添加到数字中(这会导致错误)。
你应该试试这个:
class Pizza:
def __init__(self, name, diameter, price, slices):
self.name = name
self.diameter = diameter
self.price = price
self.slices = slices
def myfunc(self):
print("Your", self.name, "pizza has a diameter of", self.diameter, "inches, a price of", self.price, "and", self.slices, "slices per pie")
和
from Pizza import Pizza
appetizer = Pizza("Pepperoni", 16, 10.50, 10)
appetizer.myfunc()
输出:
Your Pepperoni pizza has a diameter of 16 inches, a price of 10.5 and 10 slices per pie
好问题!可以使对象的名称成为 str 函数:
class Pizza:
def __init__(self, name, diameter, price, slices):
self.name = name
self.diameter = diameter
self.price = price
self.slices = slices
def __repr__(self):
return "Your {} pizza has a diameter of {} inches, a price of {} and {} slices per pie".format(self.name,self.diameter,self.price,self.slices)
appetizer = Pizza("Pepperoni", 16, 10.50, 10)
print(appetizer)
所以我在 python 中创建了一个名为 Pizza 的 class,它有 4 个参数...我希望能够插入参数然后打印它,以便它以一个干净的句子例如,如果我将其放入终端---
from Pizza import Pizza
appetizer = Pizza("Pepperoni", 16, 10.50, 10)
print(appetizer)
我想得到这个结果--- 您的意大利辣香肠比萨饼直径为 16 英寸,价格 10.5 美元,每个馅饼 10 片
不幸的是,在我的代码中,当我打印出变量时,我一直得到这个---
<Pizza.Pizza object at 0x7f17b762f650>
有人知道为什么会这样吗?我的代码如下
class Pizza:
def __init__(self, name, diameter, price, slices):
self.name = name
self.diameter = diameter
self.price = price
self.slices = slices
def myfunc(self):
print("Your" + self.name + "pizza has a diameter of" + self.diameter + "inches, a price of" + self.price + "and" + self.slices + "slices per pie")
尝试
from Pizza import Pizza
appetizer = Pizza("Pepperoni", 16, 10.50, 10).myfunc()
#you don't need to print appetizer because myfunc does it already
另外,将 class 披萨改成这个
class Pizza:
def __init__(self, name, diameter, price, slices):
self.name = name
self.diameter = diameter
self.price = price
self.slices = slices
def myfunc(self):
print("Your " + self.name + " pizza has a diameter of " + str(self.diameter) + " inches, a price of " + str(self.price) + " and " + str(self.slices) + " slices per pie")
所以它不会导致“类型错误:只能将 str(不是“int”)连接到 str”错误
您告诉 Python 打印 Pizza 对象,所以它很乐意遵从 :)。同样在您的 myfunc()
方法中,您将字符串添加到数字中(这会导致错误)。
你应该试试这个:
class Pizza:
def __init__(self, name, diameter, price, slices):
self.name = name
self.diameter = diameter
self.price = price
self.slices = slices
def myfunc(self):
print("Your", self.name, "pizza has a diameter of", self.diameter, "inches, a price of", self.price, "and", self.slices, "slices per pie")
和
from Pizza import Pizza
appetizer = Pizza("Pepperoni", 16, 10.50, 10)
appetizer.myfunc()
输出:
Your Pepperoni pizza has a diameter of 16 inches, a price of 10.5 and 10 slices per pie
好问题!可以使对象的名称成为 str 函数:
class Pizza:
def __init__(self, name, diameter, price, slices):
self.name = name
self.diameter = diameter
self.price = price
self.slices = slices
def __repr__(self):
return "Your {} pizza has a diameter of {} inches, a price of {} and {} slices per pie".format(self.name,self.diameter,self.price,self.slices)
appetizer = Pizza("Pepperoni", 16, 10.50, 10)
print(appetizer)