[Tutor] use of __new__
spir
denis.spir at free.fr
Fri Jun 12 16:03:26 CEST 2009
Le Fri, 12 Jun 2009 09:05:35 -0400,
Kent Johnson <kent37 at tds.net> s'exprima ainsi:
> On Fri, Jun 12, 2009 at 8:37 AM, Lie Ryan<lie.1296 at gmail.com> wrote:
> >>>> class Normal(object):
> > ... def __new__(cls, arg):
> > ... if arg:
> > ... return Special(arg)
> > ... else:
> > ... ret = super(Normal, cls).__new__(cls)
> > ... ret.__init__(arg)
> > ... return ret
>
> I think the else case should be
> ret = super(Normal, cls).__new__(cls, arg)
> return ret
>
> i.e. pass the same args to super.__new__() and don't explicitly call
> __init__().
>
> Kent
Holà, Kent,
Don't you have a comment in the 'if' case, too? Namely that __init__ is not invoked explicitely, while the docs clearly state:
<< f __new__() returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().
If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.>>
Denis
------
la vita e estrany
More information about the Tutor
mailing list