abc.abstractmethod + property

发布时间 2023-12-11 22:27:11作者: lightsong

abc.abstractmethod + property

https://stackoverflow.com/questions/14671095/abc-abstractmethod-property

 

import abc

class FooBase(metaclass=abc.ABCMeta):

    @property
    @abc.abstractmethod
    def greet(self):
        """ must be implemented in order to instantiate """
        pass

    @property
    def greet_comparison(self):
        """ must be implemented in order to instantiate """
        return 'hello'

class Foo(FooBase):
    @property
    def greet(self):
        return 'hello'

abstract-class

https://codefather.tech/blog/python-abstract-class/

from abc import ABC, abstractmethod

class Aircraft(ABC):

    @abstractmethod
    def fly(self):
        pass

    @abstractmethod
    def land(self):
        print("All checks completed")

class Jet(Aircraft):

    def fly(self):
        print("My jet is flying")

    def land(self):
        super().land()
        print("My jet has landed")