`staticmethod` and `abc.abstractmethod`: Will it blend?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities
--
Chapters
00:00 Question
00:59 Accepted answer (Score 361)
01:27 Answer 2 (Score 40)
01:42 Answer 3 (Score 16)
03:11 Answer 4 (Score 6)
03:49 Thank you
--
Full question
https://stackoverflow.com/questions/4474...
Question links:
[abc.abstractmethod]: http://docs.python.org/library/abc.html#...
Accepted answer links:
[possible to combine]: https://docs.python.org/3/library/abc.ht...
[version 3.3]: https://docs.python.org/3/library/abc.ht...
Answer 4 links:
[Python Issue 5867]: http://bugs.python.org/issue5867
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #abstractclass #staticmethods
#avk47
ACCEPTED ANSWER
Score 398
Starting with Python 3.3, it is possible to combine @staticmethod and @abstractmethod, so none of the other suggestions are necessary anymore:
@staticmethod
@abstractmethod
def my_abstract_staticmethod(...):
Further @abstractstatic is deprecated since version 3.3.
ANSWER 2
Score 41
class abstractstatic(staticmethod):
__slots__ = ()
def __init__(self, function):
super(abstractstatic, self).__init__(function)
function.__isabstractmethod__ = True
__isabstractmethod__ = True
class A(object):
__metaclass__ = abc.ABCMeta
@abstractstatic
def test():
print 5
ANSWER 3
Score 16
This will do it:
>>> import abc
>>> abstractstaticmethod = abc.abstractmethod
>>>
>>> class A(object):
... __metaclass__ = abc.ABCMeta
... @abstractstaticmethod
... def themethod():
... pass
...
>>> a = A()
>>> Traceback (most recent call last):
File "asm.py", line 16, in <module>
a = A()
TypeError: Can't instantiate abstract class A with abstract methods test
You go "Eh? It just renames @abstractmethod", and this is completely correct. Because any subclass of the above will have to include the @staticmethod decorator anyway. You have no need of it here, except as documentation when reading the code. A subclass would have to look like this:
>>> class B(A):
... @staticmethod
... def themethod():
... print "Do whatevs"
To have a function that would enforce you to make this method a static method you would have to subclass ABCmeta to check for that and enforce it. That's a lot of work for no real return. (If somebody forgets the @staticmethod decorator they will get a clear error anyway, it just won't mention static methods.
So in fact this works just as well:
>>> import abc
>>>
>>> class A(object):
... __metaclass__ = abc.ABCMeta
... @abc.abstractmethod
... def themethod():
... """Subclasses must implement this as a @staticmethod"""
... pass
Update - Another way to explain it:
That a method is static controls how it is called. An abstract method is never called. And abstract static method is therefore a pretty pointless concept, except for documentation purposes.
ANSWER 4
Score 6
This is currently not possible in Python 2.X, which will only enforce the method to be abstract or static, but not both.
In Python 3.2+, the new decoratorsabc.abstractclassmethod and abc.abstractstaticmethod were added to combine their enforcement of being abstract and static or abstract and a class method.