Hacking with __new__

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Tue Jul 24 12:20:34 CEST 2007


Duncan Booth a écrit :
(snip)
> I think what you want for Bar is something more along the lines:

(snip)

> class Bar(Foo):
>     def __new__(cls, a, b, c, *args):
>         print 'Bar.__new__', len(args)
>         target = cls
You don't use 'target' anywhere...

>         if not args:
>             cls = Zoo
>         obj = super(Bar, cls).__new__(cls, a, b, c, *args)
>         if args:
>             return obj
> 
>         obj.__init__(a, b, c, 7)

IIRC, __new__ is supposed to return the newly created object - which you 
are not doing here.

class Bar(Foo):
     def __new__(cls, a, b, c, *args):
         print 'Bar.__new__', len(args)
         if not args:
             cls = Zoo
         obj = super(Bar, cls).__new__(cls, a, b, c, *args)
         if not args:
             obj.__init__(a, b, c, 7)
         return obj





More information about the Python-list mailing list