タグ

pythonとmetaclassに関するishideoのブックマーク (3)

  • __new__と__init__とメタクラスと - Qiita

    # 1. __new__でインスタンスをreturnしない場合 # ref. https://docs.python.org/2/reference/datamodel.html#object.__new__ class A(object): def __new__(cls): print('new') # return super().__new__(cls) def __init__(self): print('init') super().__init__() >>> A() new # __new__は呼ばれ、__init__は呼ばれない # 2. サブクラスで親クラスの__init__を明示的に呼び出さない場合 class B(A): pass ... >>> B() new # クラスAの__new__は呼ばれ、__init__は呼ばれない 特に2のケースは、クラスが継承して使

    __new__と__init__とメタクラスと - Qiita
  • Metaclasses in Python 1.5

    Notice: This page displays a fallback because interactive scripts did not run. Possible causes include disabled JavaScript or failure to load scripts or stylesheets. (A.k.a. The Killer Joke :-) (Postscript: reading this essay is probably not the best way to understand the metaclass hook described here. See a message posted by Vladimir Marangozov which may give a gentler introduction to the matter.

    Metaclasses in Python 1.5
  • 時間城年代記:メタクラスで遊ぶ(2)

    実は前回は、メタクラス(カスタムメタクラス)で一番力を発揮する__new__メソッドの話と、__call__メソッドの話をわざと省いた。 いや、えらそうなコトを言っておきながら、自分でも少し混乱していたので、こっそりと色々試していたのだ。 で、確認終了したので、ここにまとめる。 (2007.3.2修正しました) ■__new__メソッドについて メタクラスの話に入る前に、確認を兼ねて__new__組み込みメソッドについて。 Pythonでクラスを定義する場合は、__init__メソッドで初期化するのが普通だが、生成過程を制御したい場合には、__new__組み込みメソッドを使用する。 __new__は、__init__と全く同様に引数を設定し、クラスオブジェクトの関数呼び出しの形で使用される。 ただし、二点ほど注意することがある。 一点は「戻り値を設定しない」__init__とは異なり、__

  • 1