En Python 3.6, Disons que j'ai une classe abstraite MyAbstractClass
Comment écrire à une propriété abstraite en Python 3.4+
from abc import ABC, abstractmethod
class MyAbstractClass(ABC):
@property
@abstractmethod
def myProperty(self):
pass
et une classe MyInstantiatableClass
en héritent. Alors, comment écrire sur la propriété myProperty
lors de l'instanciation d'un objet de cette classe? J'aimerais pouvoir définir et obtenir myProperty
. Ci-dessous ne fonctionne pas.
from MyAbstractClass import MyAbstractClass
class MyInstantiatableClass(MyAbstractClass):
def __init__(self, desiredValueOfMyProperty):
????
@myProperty.setter
def myProperty(self, desiredValueOfMyProperty): # value coming from __init__
self._myProperty = desiredValueOfMyProperty
Et une fonction principale, par exemple,
from MyInstantiatableClass import MyInstantiatableClass
def main():
MyInstantiatableClass(3) # 3 is the desiredValueOfMyProperty for this instantiation
MyInstantiatableClass(5) # 5 is the desiredValueOfMyProperty for this instantiation
Implémentez-vous _only_ le setter, sans getter? –
J'aimerais pouvoir définir et obtenir 'myProperty' à' MyInstantiatableClass'. – Bora
Définissez la propriété de la même manière que vous le feriez normalement. – user2357112