python - Python 程序将 self 计为参数,并且 a 导致 TypeError
问题描述
class Shirt:
def __init__(self, shirt_color, shirt_size, shirt_style, shirt_price):
self.color = shirt_color
self.size = shirt_size
self.style = shirt_style
self.price = shirt_price
def change_price(self, new_price):
self.price = new_price
def discount(self, discount):
return self.price * (1 - discount)
from shirt import Shirt
shirt_one = Shirt('red', 'M', 'long-sleeved, 45')
shirt_two = Shirt('orange', 'S', 'short-sleeved, 30')
print(shirt_one.price)
print(shirt_two.color)
shirt_two.change_price(45)
print(shirt_two.price)
TypeError: __init__() 正好需要 5 个参数(给定 4 个)
解决方案
shirt_one = Shirt('red', 'M', 'long-sleeved, 45')
shirt_two = Shirt('orange', 'S', 'short-sleeved, 30')
应该是
shirt_one = Shirt('red', 'M', 'long-sleeved', '45')
shirt_two = Shirt('orange', 'S', 'short-sleeved', '30')
您期待 4 个参数(如果包含,则为 5 个)
def __init__(self, shirt_color, shirt_size, shirt_style, shirt_price):
但你传递了 3 个参数。
推荐阅读
- python - 图像未显示在 tkinter 画布上
- mysql - 如何在 mysql 和“全有或全无”模式下执行 N 次插入
- swift - Xcode 不接收 pod 更新
- algorithm - 基数排序是复杂性算法 P 还是 NP 的一个例子?
- android-studio - Android Studio Editor 3.4.1 中的类名称未着色
- firebase - 使用 CollectionGroupQuery 的子集合中的 Firestore 写入限制
- rspec - 如何让 rspec 加载 config.ru?
- c++ - 如何访问多映射 C++ 中的特定元素
- neo4j - 路径中心的 Neo4j / Cypher 节点
- selenium - 使用 selenium python 在 youtube 文本字段中输入自定义文本