Pythonクラス中_new__()メソッドと__init__()メソッドの違い

3533 ワード

一._new__()メソッド
公式ドキュメント:object. __new__ (cls[, ...])
Called to create a new instance of class cls.  __new__()  is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of  __new__()  should be the new object instance (usually an instance of cls).
Typical implementations create a new instance of the class by invoking the superclass’s  __new__()  method using  super().__new__(cls[, ...])  with appropriate arguments and then modifying the newly-created instance as necessary before returning it.
If  __new__()  returns an instance of cls, then the new instance’s  __init__()  method will be invoked like  __init__(self[, ...]) , where self is the new instance and the remaining arguments are the same as were passed to  __new__() .
If  __new__()  does not return an instance of cls, then the new instance’s  __init__()  method will not be invoked. __new__()  is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.
翻訳:
クラスclsを作成(転送)するための新しいインスタンス._new__()は、インスタンスが要求されたクラスを最初のパラメータとし、クラス呼び出し時に渡されたパラメータを残りのパラメータとする静的メソッドです.new__()の戻り値は、新しいオブジェクトインスタンスである必要があります.(通常はクラスclsのインスタンスです).
クラスが新しいインスタンスを作成する典型的な実装はsuper()を使用することです.new__(cls[, ...])文は適切なパラメータと組み合わせて親クラスの__を呼び出します.new__方法.必要に応じて、インスタンスを返す前にインスタンスを変更できます.
もし__new__()クラスclsのインスタンスを返すと、そのインスタンスの_init__()メソッドは__のようになりますinit__(self[, ...])このように呼び出されます.ここでselfは新しいインスタンスであり、残りのパラメータは__に渡される.new__()のパラメータです.
もし__new__()クラスclsのインスタンスが返されない場合、新しいインスタンスの_init__()メソッドは実行されません.
使用__new__()メソッドは主にint,str,tupleなどの可変タイプのサブクラスカスタマイズインスタンスを許可するためである.クラスをカスタマイズする場合は、メタクラス(metaclass)で書き換えることもできます.
二._init__()メソッド
公式ドキュメント:object. __init__ (self[, ...])
Called after the instance has been created (by  __new__() ), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an  __init__()  method, the derived class’s  __init__()  method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example:  super().__init__([args...]) .
Because  __new__()  and  __init__()  work together in constructing objects ( __new__()  to create it, and  __init__()  to customize it), no non- None  value may be returned by  __init__() ; doing so will cause a  TypeError  to be raised at runtime.
翻訳:
__init__()メソッドは_new__()メソッドがインスタンスを作成すると、インスタンスは呼び出し元に返される前に呼び出されます.そのパラメータは、式を構築するパラメータに渡されます.ベースクラスに__がある場合init__()メソッド、派生クラスの_init__()メソッド(ある場合)は、呼び出しベースクラスの__を表示する必要があります.init_-()方法、例えば:super().__init__([args...]).
なぜならnew__()メソッドと__init__()メソッドは、オブジェクトの作成時に一緒に作業します(_new_()オブジェクトの作成、_init__()カスタムオブジェクト)、そして_init__()メソッドはNone以外の値を返す可能性があります.もしそうであれば、実行時にTypeErrorが発生する可能性があります.
三.区別する
__new__()インスタンスを作成し、_init__()はインスタンスの初期化を担当します.
四.参考資料
[1]python公式ドキュメント