`staticmethod` 및`abc.abstractmethod` : 혼합 될까요?
내 Python 앱에서 a staticmethod
및 abc.abstractmethod
. 어떻게해야합니까?
두 데코레이터를 모두 적용 해 보았지만 작동하지 않습니다. 이렇게하면 :
import abc
class C(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
@staticmethod
def my_function(): pass
예외가 발생하고 * 이렇게하면 :
class C(object):
__metaclass__ = abc.ABCMeta
@staticmethod
@abc.abstractmethod
def my_function(): pass
추상 방법은 적용되지 않습니다.
추상 정적 메서드를 어떻게 만들 수 있습니까?
* 예외 :
File "c:\Python26\Lib\abc.py", line 29, in abstractmethod
funcobj.__isabstractmethod__ = True
AttributeError: 'staticmethod' object has no attribute '__isabstractmethod__'
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
시작 파이썬 3.3 ,이다 결합하는 것이 가능 @staticmethod
하고 @abstractmethod
, 다른 제안 중 어느 것도 더 이상 필요하지 않도록 :
@staticmethod
@abstractmethod
def my_abstract_staticmethod(...):
이렇게하면됩니다.
>>> 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.
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.
참고URL : https://stackoverflow.com/questions/4474395/staticmethod-and-abc-abstractmethod-will-it-blend
'program story' 카테고리의 다른 글
반올림하지 않고 부동 소수점을 문자열로 변환 (0) | 2020.09.25 |
---|---|
stdcall 및 cdecl (0) | 2020.09.25 |
Composer에는 로컬 패키지가 필요합니다. (0) | 2020.09.24 |
매개 변수가있는 기본 클래스 생성자를 통한 상속 (0) | 2020.09.24 |
프라 미스 함수에 매개 변수를 전달하는 방법 (0) | 2020.09.24 |