[Python-Dev] Odd new-style class __new__ behavior
Michael Hudson
mwh@python.net
30 Mar 2002 18:33:12 +0000
Guido van Rossum <guido@python.org> writes:
> > On 30 Mar 2002, Michael Hudson wrote:
> > > Kevin Jacobs <jacobs@penguin.theopalgroup.com> writes:
> > > > Suppose I define:
> > > >
> > > > class Foo(object):
> > > > def __new__(cls):
> > > > return 1
> > > >
> > > > class Bar(object):
> > > > def __new__(cls):
> > > > return [1,2,3]
> > > >
> > > > Python 2.2 returns:
> > > > print Foo()
> > > > > 1
> > > > print Bar()
> > > > > []
> > > >
> > > > I would expect that Bar() should return [1,2,3]. Am I running into
> > > > some clever undocumented feature or a bug?
> > >
> > > Is tp_init being called on the returned list?
>
> Correct.
Goody. I always like it when my wild guesses turn out to be accurate :)
[...]
> Correct behavior is not to return a different type unless you know
> what its __init__ does.
>>> d = {1:1}
>>> d.__init__()
>>> d
{1: 1}
>>> l = [1]
>>> l.__init__()
>>> l
[]
This is at least a little odd, I'd have said. Is this just a fluke of
implementation?
It's not like I really care, though.
Cheers,
M.
--
In the 1950s and 60s there was a regular brain drain of young
Australians from the cities to London, but it was because of
money, culture and opportunity, not spiders.
-- Al Grant, ucam.chat, from Owen Dunn's review of the year