類属性.¶
- class astropy.utils.decorators.classproperty(fget=None, doc=None, lazy=False)[ソース]¶
ベースクラス:
property似たような
propertyただし,クラスクラス属性を許す.すなわち,そのgetterは類似している.classmethodそれがそうです。包装方法は明示的に使用することができる
classmethod装飾器(この装飾器になる前の装飾器)、またはclassmethod(この修飾子を使用することによって、暗黙的である)を省略することができる。注釈
クラス属性は read-only 属性です。Python記述子の動作方式の微妙な点のため、現在は属性の書き込み/削除は許可されていません。このような属性をクラス上で実現するためには,そのクラスのメタクラスを実現しなければならない.
- パラメータ
- fget呼び戻すことができる
この属性値を計算する関数(特に装飾子として用いる場合の関数).
propertyそれがそうです。- doc文字列、オプション
属性の文書文字列であるデフォルトではgetter関数から継承される.
- lazyブル値、オプション
Trueであれば,1回だけ呼び出されるようにgetter関数を最初に呼び出して返された値をキャッシュする(属性の不活性計算用).これは似たようなものです
lazypropertyそれがそうです。♪thelazyパラメータは以下の場合にも使用可能であるclassproperty装飾器として使用される(以下の第3例を参照)。修飾子文法で使うと must キーワードパラメータとして入力する.
実例.
>>> class Foo: ... _bar_internal = 1 ... @classproperty ... def bar(cls): ... return cls._bar_internal + 1 ... >>> Foo.bar 2 >>> foo_instance = Foo() >>> foo_instance.bar 2 >>> foo_instance._bar_internal = 2 >>> foo_instance.bar # Ignores instance attributes 2
前述したように、1つは
classproperty読み取り専用属性を実現することに限定される:>>> class Foo: ... _bar_internal = 1 ... @classproperty ... def bar(cls): ... return cls._bar_internal ... @bar.setter ... def bar(cls, value): ... cls._bar_internal = value ... Traceback (most recent call last): ... NotImplementedError: classproperty can only be read-only; use a metaclass to implement modifiable class-level properties
当たる
lazyオプションは、getterを一度だけ呼び出します::>>> class Foo: ... @classproperty(lazy=True) ... def bar(cls): ... print("Performing complicated calculation") ... return 1 ... >>> Foo.bar Performing complicated calculation 1 >>> Foo.bar 1
もし子が不活性を継承したら
classpropertyサブクラスの属性を再計算します:>>> class FooSub(Foo): ... pass ... >>> FooSub.bar Performing complicated calculation 1 >>> FooSub.bar 1
方法要約
deleter(fdel)属性上の削除器を変更するための記述子.
getter\(fget)属性上のgetterの記述子を変更するために用いる.
setter(fset)属性を変更するための設定器の記述子.
方法文書