2

What is the proper way of utilizing functions that are imported in the base class of an abstract class? For example: in base.py I have the following:

import abc
import functions 

class BasePizza(object):
    __metaclass__  = abc.ABCMeta

    @abc.abstractmethod
    def get_ingredients(self):
         """Returns the ingredient list."""

Then I define the method in diet.py:

import base

class DietPizza(base.BasePizza):
    @staticmethod
    def get_ingredients():
        if functions.istrue():
            return True
        else:
            retrun False

However, if I try to run

python diet.py

I get the following:

NameError: name 'functions' is not defined

How can I get diet.py to recognize libraries imported by base.py?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2694306
  • 3,832
  • 10
  • 47
  • 95

1 Answers1

5

Abstract methods do not concern themselves with the implementation details.

If you need a specific module for your specific concrete implementation, you'll need to import this in your module:

import base
import functions


class DietPizza(base.BasePizza):
    @staticmethod
    def get_ingredients():
        return functions.istrue()

Note that a module import in multiple places doesn't cost anything extra. Python re-uses the already created module object when a module is used in multiple other modules.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343