[Python-Dev] Odd new-style class __new__ behavior

Kevin Jacobs jacobs@penguin.theopalgroup.com
2002年3月30日 12:11:50 -0500 (EST)


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?

I suspect that the object construction code would check the result of tp_new
to see if the expected type. If not, tp_init should not be called on the
instance. I suspect that this check must already be there, or else none of
these cases would work at all.
Anyhow, I won't know what is really happening for sure until Monday, when I
can run this through gdb. However, some more data points: returning
dictionaries, tuples, strings, classic object instances, and user-defined
new-style classes all seem to work. Only lists seem to behave this way.
Guido, can you clarify what the "correct behavior" should be for the above
cases? Once I know that, I will happily supply a corrective patch if one is
necessary.
Regards,
-Kevin
--
Kevin Jacobs
The OPAL Group - Enterprise Systems Architect
Voice: (216) 986-0710 x 19 E-mail: jacobs@theopalgroup.com
Fax: (216) 986-0714 WWW: http://www.theopalgroup.com

AltStyle によって変換されたページ (->オリジナル) /