Python 3.4以降、DynamicClassAttribute
という記述子があります。ドキュメントの状態:DynamicClassAttributeとは何ですか?どうすれば使用できますか?
types.DynamicClassAttribute(fget=None, fset=None, fdel=None, doc=None)
ルート
__getattr__
にクラスのアクセス属性。これは、インスタンスおよびクラスを通してアクセスするときに異なる動作をする属性を定義するために使用される記述子です。インスタンスのアクセスは通常のままですが、クラスを介した属性へのアクセスはクラスの
__getattr__
メソッドにルーティングされます。これは、AttributeError
を上げることによって行われます。これは、インスタンス上でプロパティをアクティブにでき、同じ名前のクラスに仮想属性を持つことができます(例として列挙を参照)。
バージョン3.4の新機能
それは明らかにenum moduleで使用されます。
# DynamicClassAttribute is used to provide access to the `name` and
# `value` properties of enum members while keeping some measure of
# protection from modification, while still allowing for an enumeration
# to have members named `name` and `value`. This works because enumeration
# members are not set directly on the enum class -- __getattr__ is
# used to look them up.
@DynamicClassAttribute
def name(self):
"""The name of the Enum member."""
return self._name_
@DynamicClassAttribute
def value(self):
"""The value of the Enum member."""
return self._value_
私はそのenums are a little specialを実現するが、私は、これはDynamicClassAttribute
とどのように関係するかを理解していません。これらの属性がの動的なであることは、通常のプロパティとどのように違うのですか。DynamicClassAttribute
を私の利点に使用するにはどうすればよいですか?