google app engine - Python 2.7 Combine abc.abstractmethod and classmethod -


How do I create decorator for an abstract class method in Python 2.7?

Yes, this is similar, except I would like to add staticmethod instead of abc.abstractmethod and classmethod . . Also, it seems that was abc.abstractclassmethod , but I am using Google App Engine, so I am currently limited to Python 2.7

thanks in advance.

Python 3.3 is a work example obtained from the source code in the ABC module:

  (classmethod): __isabstractmethod__ = true def __init __ (self, redeemable) callable .__ isabstractmethod__ = true super (abstractclassmethod, self) .__ init __ (refundable) class DemoABC: __metaclass__ = ABCMeta @abstractclassmethod def from_int (CLS, n): return cls () class DemoConcrete (DemoABC): @classmethod def from_int (CLS, n): return cls (2 * N) def __init __ (self, n): Print 'Started "With being, n  

Looks like this is going on:

  & gt; & Gt; & Gt; D = Democoncrit (5) # By calling a concrete __ignit (__), profit 5) with the initial 5 & gt; & Gt; & Gt; D = DemoConcrete.from_int (5) # taking advantage of calling a solid from_int ()) 10> & Gt; & Gt; DemoABC () # fails because From_int () Abstract Traceback (most recent call last): ... Type error: can not instantiate abstract methods abstract class demoABC from_int> & gt; & Gt; & Gt; DemoABC.from_int (5) # because from_int () not Traceback (most recent call last) failed to apply: ... writing error: can not instantiate abstract class DemoABC with abstract methods from_int  

note that the last example fails because cls () instantiate will not Abisimta stops immediately before the time of those classes, which define all the required abstract methods Do not do it.

Another way to trigger a failure when the from_int () is called abstract class method is an exception:

  class DemoABC: __metaclass__ = ABCMeta @abstractclassmethod def from_int (CLS, N) increased NotImplementedError  

design ABCMeta any essence of being called a uninstantiated class If there is no attempt to stop the method, then it is up to you to speed up a failure by applying cls () as usually classmethods or an NotImp By lementedError Raised in any way, you get a good, clean failure.

This is probably tempting to write a descriptor to prevent direct calls to a clear class method, but it's difference with the overall design of ABCM eta Instead of urging, it is about examining the necessary methods before urgency).


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -