属性错误:';元组';对象没有属性';价格*args用法的问题。我做错了什么? class product(): 定义初始(自身、价格、产品id、数量): self.price=价格 self.product\u id=产品\u id self.quantity=数量 def计算_值(*stuff): 打印(总和(物料.价格*物料.数量)) a=产品(2,“a”,2) b=产品(3,“b”,3) 计算_值(a,b) 错误: 回溯(最近一次呼叫最后一次): 文件“/Users/apple/Desktop/Python/product\u inventory\u project.py”, 第17行,在 计算_值(a,b) 文件“/Users/apple/Desktop/Python/product\u inventory\u project.py”, 第11行,计算_值 打印(总和(物料.价格*物料.数量)) AttributeError:“tuple”对象没有属性“price”
我做错了什么?我觉得calculate_值中的*args导致了问题,但我看不到故障。非常感谢 您需要遍历属性错误:';元组';对象没有属性';价格*args用法的问题。我做错了什么? class product(): 定义初始(自身、价格、产品id、数量): self.price=价格 self.product\u id=产品\u id self.quantity=数量 def计算_值(*stuff): 打印(总和(物料.价格*物料.数量)) a=产品(2,“a”,2) b=产品(3,“b”,3) 计算_值(a,b) 错误: 回溯(最近一次呼叫最后一次): 文件“/Users/apple/Desktop/Python/product\u inventory\u project.py”, 第17行,在 计算_值(a,b) 文件“/Users/apple/Desktop/Python/product\u inventory\u project.py”, 第11行,计算_值 打印(总和(物料.价格*物料.数量)) AttributeError:“tuple”对象没有属性“price”,python,function,class,tuples,args,Python,Function,Class,Tuples,Args,我做错了什么?我觉得calculate_值中的*args导致了问题,但我看不到故障。非常感谢 您需要遍历内容,以访问传递的每个产品 class product(): def __init__(self, price, product_id, quantity): self.price = price self.product_id = product_id self.quantity = quantity def calculate_value(*stuf
内容
,以访问传递的每个产品
class product():
def __init__(self, price, product_id, quantity):
self.price = price
self.product_id = product_id
self.quantity = quantity
def calculate_value(*stuff):
print(sum(stuff.price*stuff.quantity))
a = product(2,"a", 2)
b = product(3, "b", 3)
calculate_value(a,b)
Error:
Traceback (most recent call last):
File "/Users/apple/Desktop/Python/product_inventory_project.py",
line 17, in <module>
calculate_value(a,b)
File "/Users/apple/Desktop/Python/product_inventory_project.py",
line 11, in calculate_value
print(sum(stuff.price*stuff.quantity))
AttributeError: 'tuple' object has no attribute 'price'
输出
def calculate_value(*stuff):
return sum(i.price * i.quantity for i in stuff)
您需要循环使用
内容
,以访问传递的每个产品
class product():
def __init__(self, price, product_id, quantity):
self.price = price
self.product_id = product_id
self.quantity = quantity
def calculate_value(*stuff):
print(sum(stuff.price*stuff.quantity))
a = product(2,"a", 2)
b = product(3, "b", 3)
calculate_value(a,b)
Error:
Traceback (most recent call last):
File "/Users/apple/Desktop/Python/product_inventory_project.py",
line 17, in <module>
calculate_value(a,b)
File "/Users/apple/Desktop/Python/product_inventory_project.py",
line 11, in calculate_value
print(sum(stuff.price*stuff.quantity))
AttributeError: 'tuple' object has no attribute 'price'
输出
def calculate_value(*stuff):
return sum(i.price * i.quantity for i in stuff)
当您使用
*args
(或*stuff
)时,它是传入的所有参数的元组(或从技术上讲是所有剩余参数的元组)。因此,当您使用两个参数调用calculate\u value(…)
时,stuff
现在是两个项的元组。如果使用一个参数调用calculate\u value(…),则stuff将是一个项的元组。无论如何,您需要对元组进行迭代(或其他操作),以获得传入的
product项。您将
东西视为
产品的一个实例,但事实并非如此。这是一个元组。因此,错误是说
tuple对象没有
price`属性。当您使用*args
(或*stuff
)时,它是传入的所有参数的元组(或从技术上讲是所有剩余参数的元组)。因此,当您使用两个参数调用calculate\u value(…)
时,stuff
现在是两个项的元组。如果使用一个参数调用calculate\u value(…),则stuff将是一个项的元组。无论如何,您需要对元组进行迭代(或其他操作),以获得传入的
product项。您将
东西视为
产品的一个实例,但事实并非如此。这是一个元组。因此,错误是说
tuple对象没有
price`属性。我明白了,python会自动将这里的内容视为一个tuple?基本上,是的。从规范上讲,它们分别被称为并表示位置参数和命名参数。我明白了,python会自动将这里的内容视为元组?基本上,是的。在规范上,这些参数分别称为并表示位置参数和命名参数。