2016-11-19 65 views
1

我原本是一個Django開發人員,現在我正在嘗試玩一些基本的純Python 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

如果這些文件在同一文件夾,你就應該能夠做'從abstract_base_class進口車'' –

回答

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

+0

這不是我的問題。請檢查錯誤 –

+0

謝謝!這就是訣竅! –

+0

@Dean Christian:歡迎,很高興能有所幫助 – Serjik