CKTIS / Python
abc.abstractmethod
Dekorator označava član kao apstraktan u klasama koje koriste ABCMeta.
Verzija: Python 3.14; repository image, production runtime not attested
Sintaksa
@property
@abstractmethod
def unit(self): ...Dekorator označava član kao apstraktan u klasama koje koriste ABCMeta.
abc.abstractmethod
Uz property ili classmethod abstractmethod treba biti najbliže def. Apstraktno tijelo može sadržavati kod koji izvedba u podklasi poziva preko super().
Base zahtijeva unit, a Scale proširuje osnovni m u mm.
from abc import ABC,abstractmethod
class Base(ABC):
@classmethod
@abstractmethod
def make(cls): return cls()
assert Base.__abstractmethods__=={'make'}
class Concrete(Base):
@classmethod
def make(cls): return super().make()
assert type(Concrete.make()) is Concrete
Primjeri
from abc import ABC, abstractmethod
class Base(ABC):
@property
@abstractmethod
def unit(self): return "m"
class Scale(Base):
@property
def unit(self): return super().unit + "m"
print(sorted(Base.__abstractmethods__), Scale().unit)
Rezultat
['unit'] mm
Base zahtijeva unit, a Scale proširuje osnovni m u mm.