次のスクリプト例を見てください。
class A(object):
@classmethod
def one(cls):
print("I am class")
@staticmethod
def two():
print("I am static")
class B(object):
one = A.one
two = A.two
B.one()
B.two()
このスクリプトを Python 2.7.11 で実行すると、次のようになります。
I am class
Traceback (most recent call last):
File "test.py", line 17, in <module>
B.two()
TypeError: unbound method two() must be called with B instance as first argument (got nothing instead)
@classmethod デコレータはクラス全体で保持されているようですが、 @staticmethod は保持されていません。
Python 3.4 は期待どおりに動作します。
I am class
I am static
Python2 が @staticmethod を保持しないのはなぜですか? 回避策はありますか?
編集:クラスから2つを取り出す(そして@staticmethodを保持する)ことはうまくいくようですが、それでも私には奇妙に思えます。