相对导入错误

时间:2016-11-19 06:54:48

标签: python

我原来是一名Django开发人员,现在我正在尝试使用一些基本的纯python OOP。在我尝试导入一个在我的子类上继承的抽象基类之前,我一切都很好

错误是:

Traceback (most recent call last):
  File "inheritance.py", line 1, in <module>
    from . abstract_base_class import Vehicle
ValueError: Attempted relative import in non-package

我的文件夹结构是:

├── OOP
│   ├── __init__.py
│   ├── abstract_base_class.py
│   ├── classes.py
│   ├── inheritance.py
│   └── inheritance.pyc

我的abstract_base_class.py是:

from abc import ABCMeta, abstractmethod


class Vehicle(object):
    """
    @brief      An example of Abstract Base Class
    @attrs      base_sale_price:
                wheels:
    """

    __metaclass__ = ABCMeta
    """
    - A metaclass is the class of a class. Like a class defines how an
    instance of the class behaves, a metaclass defines how a class behaves.
    A class is an instance of a metaclass.
    - This declares that the class is abstract
        -- This will not run as long as there is an abstractmethod decorator
        -- Try running this and it will result to:
            --- TypeError: Can't instantiate abstract class Vehicle with
                abstract methods vehicle_types
    - Try removing this and the class will work just fine
    """

    base_sale_price = 0
    wheels = 0

    def __init__(self, miles, make, model, year, sold_on, *args, **kwargs):
        """
        @brief      Constructs the object.
        @attrs      wheels: An integer representing the number of wheels the vehicle has.
                    miles: The integral number of miles driven on the vehicle.
                    make: The make of the vehicle as a string.
                    model: The model of the vehicle as a string.
                    year: The integral year the vehicle was built.
                    sold_on: The date the vehicle was sold.
        """

        self.miles = miles
        self.make = make
        self.model = model
        self.year = year
        self.sold_on = sold_on

    def sale_price(self):
        """
        @brief      TODO
        """

        if self.sold_on is not None:
            return 0.0
        return 5000.0 * self.wheels

    def purchase_price(self):
        """
        @brief      TODO
        """

        if self.sold_on is None:
            return 0.0
        return self.base_sale_price - (.10 * self.miles)

    @abstractmethod
    def vehicle_types(self):
        """
        @brief
            The decorator enforces the child classes to define this method
        """

        pass

x = Vehicle(1, 2, 3, 4, 5)
print x.miles
print x.vehicle_types()

我的inheritance.py是:

from . abstract_base_class import Vehicle


class Car(Vehicle):
    """
    @brief      An example class inheriting the Vehicle Base Class
    @attrs      base_sale_price:
                wheels:
    """

    base_sale_price = 8000
    wheels = 4

    def vehicle_type(self):
        """
        @brief      TODO
        """

        return 'car'


class Truck(Vehicle):
    """
    @brief      An example class inheriting the Vehicle Base Class
    """

    base_sale_price = 10000
    wheels = 4

    def vehicle_type(self):
        """
        @brief      TODO
        """

        return 'truck'


class Motorcycle(Vehicle):
    """
    @brief      An example class inheriting the Vehicle Base Class
    """

    base_sale_price = 2000
    wheels = 2

    def vehicle_type(self):
        """
        @brief      TODO
        """

        return 'motorcycle'

x = Car()
print x.wheels

我希望删除错误,以便继续练习我的OOP代码

1 个答案:

答案 0 :(得分:1)

在OOP术语中,您无法实例化Abstract Class 你有一个名为Vehicle的类,它有一个名为vehicle_types的抽象方法,所以Vehicle是抽象类,你尝试在abstract_base_class.py中实例化你的类

x = Vehicle(1, 2, 3, 4, 5)  # This is false, Vehicle is abstract
  

第二:   取代

from . abstract_base_class import Vehicle

from abstract_base_class import Vehicle

或使用OOP作为python -m的包,请参阅

How to fix “Attempted relative import in non-package” even with init.py