共有方法¶
- class astropy.utils.decorators.sharedmethod[ソース]¶
ベースクラス:
classmethod
これは例示的な方法と
classmethod
同じ名前を共有する。使用時
sharedmethod
クラスアスペクトで定義されたメソッドでは,インスタンス上で呼び出すこともできるし,クラス上で呼び出すことも可能である.前者の場合、その行動は、通常のインスタンス方法と同様である(インスタンスへの参照は、自動的に第1の転送として使用されるself
メソッドのパラメータ):>>> class Example: ... @sharedmethod ... def identify(self, *args): ... print('self was', self) ... print('additional args were', args) ... >>> ex = Example() >>> ex.identify(1, 2) self was <astropy.utils.decorators.Example object at 0x...> additional args were (1, 2)
後者の場合は
sharedmethod
クラスから直接呼び出すと,その行為は類似しているclassmethod
**>>> Example.identify(3, 4) self was <class 'astropy.utils.decorators.Example'> additional args were (3, 4)
これはより高度な使い方にも対応しています
classmethod
実現は単独で作成することができる.もしクラスの学生が 元類. 与力を持つ.sharedmethod
メタクラス上のバージョンは、以下のように依頼されます。>>> class ExampleMeta(type): ... def identify(self): ... print('this implements the {0}.identify ' ... 'classmethod'.format(self.__name__)) ... >>> class Example(metaclass=ExampleMeta): ... @sharedmethod ... def identify(self): ... print('this implements the instancemethod') ... >>> Example().identify() this implements the instancemethod >>> Example.identify() this implements the Example.identify classmethod