Python 3は、両方がオーバーライドされたときに、どのようにobject.__new__
とobject.__init__
が引数に反応するかを変更しました。クラスがオーバーライドする(またはオーバーライドするメソッドを継承する)場合、余分な引数を受け取った場合、object.__init__
とobject.__new__
の両方にobject.__init__
とobject.__new__
が例外をスローします。 Python 2では、DeprecationWarning(デフォルトでは抑制)が与えられていました。
tuple
は、独自の__init__
を持っていません。それはobject.__init__
を継承しているので、実際にはobject.__init__
に多くの引数を渡しています。object.__init__
は受け取りません。 Python 2はあなたに(抑制された)警告を与えていました。そしてPython 3はそれをエラーにしています。
コードがobject.__init__
と、余分な引数のobject.__new__
の微妙な取り扱いを説明するのは良い仕事をしていませんコメントがあります
/* You may wonder why object.__new__() only complains about arguments
when object.__init__() is not overridden, and vice versa.
Consider the use cases:
1. When neither is overridden, we want to hear complaints about
excess (i.e., any) arguments, since their presence could
indicate there's a bug.
2. When defining an Immutable type, we are likely to override only
__new__(), since __init__() is called too late to initialize an
Immutable object. Since __new__() defines the signature for the
type, it would be a pain to have to override __init__() just to
stop it from complaining about excess arguments.
3. When defining a Mutable type, we are likely to override only
__init__(). So here the converse reasoning applies: we don't
want to have to override __new__() just to stop it from
complaining.
4. When __init__() is overridden, and the subclass __init__() calls
object.__init__(), the latter should complain about excess
arguments; ditto for __new__().
Use cases 2 and 3 make it unattractive to unconditionally check for
excess arguments. The best solution that addresses all four use
cases is as follows: __init__() complains about excess arguments
unless __new__() is overridden and __init__() is not overridden
(IOW, if __init__() is overridden or __new__() is not overridden);
symmetrically, __new__() complains about excess arguments unless
__init__() is overridden and __new__() is not overridden
(IOW, if __new__() is overridden or __init__() is not overridden).
However, for backwards compatibility, this breaks too much code.
Therefore, in 2.6, we'll *warn* about excess arguments when both
methods are overridden; for all other cases we'll use the above
rules.
*/
をあなたは '__new__'を使用していない理由を私は聞いてもいいですか?それは行く方法だと思われる。 – TigerhawkT3
@ TigerhawkT3私は今です:)しかし、私はまだこれについて興味があります – goncalopp