[Tutor] __init__ for class instantiation?
Wed Apr 30 12:32:01 2003
>> ZA> The problem here (as with __new__) is that __init__ will be
>> with ZA> every creation of a Player instance.
>> It seems that you are wrong here---__new__ is called only once
>> class is defined.
>I created a new class, and defined __new__, but it doesn't get called
>Python, as far as I can tell. Here's my code:
> def __new__ (cls):
> print (cls)
>Even when I create the first instance (d = C()), I don't get any
I didn't see a response to this yet, so let me chime in.
The __new__ magic method is a feature of new-style Python classes. As
far as I know, a __new__ method defined in an old-style class is just
like any other method. (Except for the funny name :)
count = 0
C.count += 1
>>> c1 = C()
<__main__.C object at 0x00923668>
>>> c2 = C()
<__main__.C object at 0x00933A50>
This perhaps is an odd way to use __new__ - I made this only to
demonstrate that __new__ is called for every instantiation of a
PS: Although there's nothing wrong with the Python docs, I really liked
the section on classes in Alex Martelli's book "Python in a Nutshell" -
it's a good reference.