new style classes, __new__, __init__

Torsten Mohr tmohr at s.netic.de
Tue Sep 16 15:21:18 EDT 2008


Hello,

> This way __new__ is not called, if i remove __init__ then
> there are too many parameters to __new__, if i add a parameter
> to __new__ then it says that __new__ does not take arguments.

I just found an article that describes it better, this example works:

class C2(object):
    def __new__(cls, a):
        obj = object.__new__(cls)
        print "new called"
        obj.a = 8

        return obj

    __new__ = staticmethod(__new__)


    def __init__(self, a):
        print "init called"
        self.a = a

    def fct(self):
        print self.a


a = C2(7)
a.fct()


Best regards,
Torsten.




More information about the Python-list mailing list