Re[2]: [Tutor] __init__ for class instantiation?

antonmuhin at rambler.ru antonmuhin at rambler.ru" <antonmuhin@rambler.ru
Wed Apr 30 02:57:03 2003


Hello Zak,

Monday, April 28, 2003, 9:26:40 PM, you wrote:

ZA> I created a new class, and defined __new__, but it doesn't get called from
ZA> Python, as far as I can tell. Here's my code:

ZA> class C:
ZA>      def __new__ (cls):
ZA>           print (cls)

ZA> Even when I create the first instance (d = C()), I don't get any output.

Yes, except for cases (if I'm correct) when you inherit from immutable
classes:

class Foo(tuple):
      def __new__(self): print "new"

Foo()

should print "new".

However, I might miss the point: method __new__ should be defined not
in the class, but in the metaclass. In this case it gets called only
once after the class declaration is over.

-- 
Best regards,
 anton                            mailto:antonmuhin@rambler.ru