私はクラス変数を検証し、フォーマットしようとしています。クラスはABCMeta
のクラスを拡張して__metaclass__
となり、まだ私の子クラスをインスタンス化できません。だから私はコードの下でこれを実行するとオブジェクトオブジェクトを出力し、 は出力しません。理由はわかります。しかし、私はそれをどうやってやるの?Pythonのフォーマットとそれをインスタンス化せずにクラス変数を検証
from abc import ABCMeta, abstractproperty
class RuleBase(object):
__metaclass__ = ABCMeta
id = None
id = abstractproperty(id)
@property
def format_id(self):
try:
_id = 'R%02d' % self.id
except TypeError:
raise TypeError("ID must be an integer")
return _id
@classmethod
def verbose(cls):
print cls.format_id
class Rule(RuleBase):
id = 1
Rule.verbose() # prints property object and not R01
私の理論では、これはうまくいくと思います。
class FormattingABCMeta(ABCMeta):
def __new__(mcl, classname, bases, dictionary):
# Format here, remove format_id property from RuleBase,
# and use FormattingABCMeta as metaclass for RuleBase.
return super(C, mcl).__new__(mcl, classname, bases, dictionary)
しかし、私はあまりにもそれをねじっている感じ。では、これをどうやって行うのですか?そして私の理解は正しいのですか?助けを借りて をいただければ幸いです。
プロパティのみ**インスタンスでは機能**、ありませんクラスで。彼らは単にクラスにバインドされていません。 'verbose'はクラスメソッドなので、' cls.format_id'を使うことはできません。 'format_id'をクラスメソッドにして呼び出す必要があります。 –
代わりに[クラスプロパティ記述子](http://stackoverflow.com/questions/5189699/how-can-i-make-a-class-property-in-python)を作成することです。 –