__new__ and __init__

Possible duplicate:
Using Python __new__ and __init__?

I am trying to figure out how to use these two methods. I know what is __new__used to create an object, but __init__for initialization. But I'm not sure what happens when I create an object.

  • Does this mean that __new__they __init__should have the same parameters?

  • What happens if I do not use the same parameters?

+5
source share
2 answers
  • Yes.

  • You will receive an error message.

( __new__ , __init__ . , , , , , __init__, , __new__.)

>>> class Foo(object):
...     def __new__(cls, x):
...         return super(Foo, cls).__new__(cls)
...     
...     def __init__(self, x, y):
...         pass
>>> Foo(1)
Traceback (most recent call last):
  File "<pyshell#260>", line 1, in <module>
    Foo(1)
TypeError: __init__() takes exactly 3 arguments (2 given)
>>> Foo(1, 2)
Traceback (most recent call last):
  File "<pyshell#261>", line 1, in <module>
    Foo(1, 2)
TypeError: __new__() takes exactly 2 arguments (3 given)
+6

() , ( ).

"", __new__ , __init__ __new__; .

python " "; *args **keyword (. * args ** kwargs ?), , __new__, __init__ , .

, ( )? , python, , ; .

+5

All Articles