【问题标题】:Python Class variable topic showing errors显示错误的 Python 类变量主题
【发布时间】:2022-11-27 15:10:18
【问题描述】:

类变量

类车:

amount_cars = 0

def __init__(self, manufacturer, model, hp):
    self.manufacturer = manufacturer
    self.model = model
    self.hp = hp
    Car.amount_cars += 1

def print_car_amount(self):
    print("Amount: {}".format(Car.amount_cars))

创建变量

myCar1 = Car("特斯拉", "Model X", 525)

打印变量

myCar1.print_info()

输出:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [37], in <cell line: 1>()
----> 1 myCar1.print_info()

AttributeError: 'Car' object has no attribute 'print_info

需要帮助查找错误

【问题讨论】:

    标签: python


    【解决方案1】:

    正如错误消息中所述。您没有名为 print_info 的方法。 可能,你正在尝试做:
    myCar1.print_car_ammount()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      相关资源
      最近更新 更多