1

I am originally a Django developer and now I'm trying to play some basic and pure python OOP. I'm all good until I tried importing an abstract base class to be inherited on my child classes

The error is:

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

My folder structure is:

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

My abstract_base_class.py is:

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()

My inheritance.py is:

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

I wish to remove the error so I can continue practicing my OOP codes

Dean Christian Armada
  • 6,724
  • 9
  • 67
  • 116

1 Answers1

1

In OOP terminology you can not instantiate an Abstract Class You have a class named Vehicle which has an abstract method named vehicle_types so Vehicle is abstract class and you try to instantiate you class in abstract_base_class.py

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

Second: replace

from . abstract_base_class import Vehicle

with

from abstract_base_class import Vehicle

or use OOP as package with python -m , refer to

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

Community
  • 1
  • 1
Serjik
  • 10,543
  • 8
  • 61
  • 70