混合使用静态方法、类方法和抽象方法
import abc class BasePizza(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def get_ingredients(self): """Returns the ingredient list.""" class Calzone(BasePizza): def get_ingredients(self, with_egg=False): egg = Egg() if with_egg else None return self.ingredients + [egg] class DietPizza(BasePizza): @staticmethod def get_ingredients(): return None
import abc class BasePizza(object): __metaclass__ = abc.ABCMeta default_ingredients = ['cheese'] @classmethod @abc.abstractmethod def get_ingredients(cls): """Returns the default ingredient list.""" return cls.default_ingredients class DietPizza(BasePizza): def get_ingredients(self): return [Egg()] + super(DietPizza, self).get_ingredients()