<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">Il giorno 15 marzo 2016 17:38, enrico franchi <span dir="ltr"><<a href="mailto:enrico.franchi@gmail.com" target="_blank">enrico.franchi@gmail.com</a>></span> ha scritto<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><span class=""><div>O a me non e' chiaro quale sia il tuo problema concreto, oppure tu non hai guardato la doc per abstractproperty.</div></span><div><br></div><div><a href="https://docs.python.org/2/library/abc.html#abc.abstractproperty" target="_blank">https://docs.python.org/2/library/abc.html#abc.abstractproperty</a></div></div></blockquote><div><br></div><div dir="ltr">In effetti lo avevo visto, e mi era sembrato un po' più appropriato che richiedere un metodo come con @abstractmethod. In realtà da Python 3.3 è deprecato in favore dell'uso combinato di @property e @abstractmethod. Il guaio di questo approccio è che le classi derivate devono implementare un metodo decorato con @property e non basta definire una proprietà (giustamente non attributo, che è più generale) come con @abstractmethod:<br><br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><font size="1"><span style="font-family:monospace,monospace">In [1]: from abc import ABC, abstractmethod</span></font><br><font size="1"><span style="font-family:monospace,monospace"></span></font><br><font size="1"><span style="font-family:monospace,monospace">In [2]: class B(ABC):</span></font><br><font size="1"><span style="font-family:monospace,monospace"> ...: @property</span></font><br><font size="1"><span style="font-family:monospace,monospace"> ...: @abstractmethod</span></font><br><font size="1"><span style="font-family:monospace,monospace"> ...: def foo(self): pass</span></font><br><font size="1"><span style="font-family:monospace,monospace"> ...: </span></font><br><font size="1"><span style="font-family:monospace,monospace"></span></font><br><font size="1"><span style="font-family:monospace,monospace">In [3]: class A(B):</span></font><br><font size="1"><span style="font-family:monospace,monospace"> ...: foo = 'bar'</span></font><br><font size="1"><span style="font-family:monospace,monospace"> ...: </span></font><br><font size="1"><span style="font-family:monospace,monospace"></span></font><br><font size="1"><span style="font-family:monospace,monospace">In [4]: A()</span></font><br><font size="1"><span style="font-family:monospace,monospace">Out[4]: <__main__.A at 0x7f2940d14160></span></font><br><font size="1"><span style="font-family:monospace,monospace"></span></font><br><font size="1"><span style="font-family:monospace,monospace">In [5]: A().bar</span></font><br><font size="1"><span style="font-family:monospace,monospace">---------------------------------------------------------------------------</span></font><br><font size="1"><span style="font-family:monospace,monospace">AttributeError Traceback (most recent call last)</span></font><br><font size="1"><span style="font-family:monospace,monospace"><ipython-input-5-41873e071d53> in <module>()</span></font><br><font size="1"><span style="font-family:monospace,monospace">----> 1 A().bar</span></font><br><font size="1"><span style="font-family:monospace,monospace"></span></font><br><font size="1"><span style="font-family:monospace,monospace">AttributeError: 'A' object has no attribute 'bar'</span></font><br><font size="1"><span style="font-family:monospace,monospace"></span></font><br><font size="1"><span style="font-family:monospace,monospace">In [6]: class C(ABC):</span></font><br><font size="1"><span style="font-family:monospace,monospace"> ...: @abstractmethod</span></font><br><font size="1"><span style="font-family:monospace,monospace"> ...: def foo(self): pass</span></font><br><font size="1"><span style="font-family:monospace,monospace"> ...: </span></font><br><font size="1"><span style="font-family:monospace,monospace"></span></font><br><font size="1"><span style="font-family:monospace,monospace">In [7]: class D(C):</span></font><br><font size="1"><span style="font-family:monospace,monospace"> ...: foo = 'bar'</span></font><br><font size="1"><span style="font-family:monospace,monospace"> ...: </span></font><br><font size="1"><span style="font-family:monospace,monospace"></span></font><br><font size="1"><span style="font-family:monospace,monospace">In [8]: D()</span></font><br><font size="1"><span style="font-family:monospace,monospace">Out[8]: <__main__.D at 0x7f2940d320f0></span></font><br><font size="1"><span style="font-family:monospace,monospace"></span></font><br><font size="1"><span style="font-family:monospace,monospace">In [9]: D.foo</span></font><br><font size="1"><span style="font-family:monospace,monospace">Out[9]: 'bar'</span></font><br></blockquote><div><br></div>Comunque <span class="">abstractproperty (o equivalentemente property + abstractmethod) si avvicina di più a quello che cercavo.<br><br></span>Poi mi è venuta in mente anche una quarta possibilità: chiamare hasattr nel metodo __new__ della classe astratta, cosa che segnala l'eccezione un po' prima che se chiamato in __init__<br><br></div><div>Grazie,<br></div><div>Marco <br clear="all"></div></div><br>-- <br><div class="gmail_signature"><div dir="ltr"><span style="font-family:monospace,monospace"></span><span style="font-family:monospace,monospace">|_|0|_|</span><br><span style="font-family:monospace,monospace"></span><span style="font-family:monospace,monospace"></span><div><span style="font-family:monospace,monospace">|_|_|0|<br>|0|0|0|<br><br></span></div></div></div>
</div></div>