简体   繁体   中英

Python Abstract Proper Way of Calling Library Imported in Base Class

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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