简体   繁体   English

NameError:未定义名称“汽车”

[英]NameError: name 'Automobile' is not defined

Not sure why I am getting NameError: name 'Automobile' is not defined. 不知道为什么我得到NameError:未定义名称'Automobile'。 Maybe someone who hasn't been staring at this for the past 20 minutes can help out. 也许在过去20分钟内一直没有凝视此事的人可以提供帮助。 I am starting to think it might be the indention, but not too sure yet. 我开始认为这可能是缩进,但还不太确定。

vehicles.py: Vehicles.py:

class Automobile:

    def __init__(self, make, model, mileage, price):
        self.__make = make
        self.__model = model
        self.__mileage = mileage
        self.__price = price

    def set_make(self, make):
        self.__make = make

    def set_model(self, model):
        self.__model = model

    def set_mileage(self, mileage):
        self.__mileage = mileage

    def set_price(self, price):
        self.__price = price

    def get_make(self):
        return self.__make

    def get_model(self):
        return self.__model

    def get_mileage(self):
        return self.__mileage

    def get_price(self):
        return self.__price

    class Car(Automobile):

        def __init__(self, make, model, mileage, price, doors):

            Automobile.__init__(self, make, model, mileage, price)

            self.__doors = doors

        def set_doors(self, doors):
            self.__doors = doors

        def get_doors(self):
            return self.__doors

car_demo.py: car_demo.py:

import vehicles

def main():
    used_car = vehicles.Car("Audi", 2007, 12500, 21500.00, 4)

    print('Make:', used_car.get_make())
    print('Model:', used_car.get_model())
    print('Mileage:', used_car.get_mileage())
    print('Price:', used_car.get_price())
    print('# of doors:', used_car.get_doors())

main()

Error: 错误:

Traceback:
import vehicles, class Automobile:, and class Car(Automobile)

NameError: name 'Automobile' is not defined

I would've thought setting Automobile as a class defines 'Automobile' Any help would be appreciated. 我本以为将“汽车”设置为一个类来定义“汽车”,将不胜感激。

I am using Python 3.5.2 IDLE 我正在使用Python 3.5.2 IDLE

I assume you didn't mean to indent the Car class inside of Automobile . 我相信你不是故意要缩进Car的内部类Automobile You should un-indent that class one level, and instead of: 您应该取消缩进该类的一级,而不是:

Automobile.__init__(self, make, model, mileage, price)

you should use: 您应该使用:

super(Car, self).__init__(make, model, mileage, price)

And if you're using Python 3, you can just do: 如果您使用的是Python 3,则可以执行以下操作:

super().__init__(make, model, mileage, price)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM