CKTIS / Python
abc.abstractmethod
Dekorator označi član kot abstrakten v razredih, ki uporabljajo ABCMeta.
Različica: Python 3.14; repository image, production runtime not attested
Sintaksa
@property
@abstractmethod
def unit(self): ...Dekorator označi član kot abstrakten v razredih, ki uporabljajo ABCMeta.
abc.abstractmethod
Pri property ali classmethod naj bo abstractmethod najbližje def. Abstraktno telo lahko vsebuje kodo, ki jo izvedba v podrazredu pokliče prek super().
Base zahteva unit in Scale razširi osnovni m v 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
Primeri
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 zahteva unit in Scale razširi osnovni m v mm.