Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18037

Re: confused about __new__

Date 2011-12-27 19:05 +0100
From Fredrik Tolf <fredrik@dolda2000.com>
Subject Re: confused about __new__
References <MPbKq.47607$cN1.25052@newsfe12.iad> <alpine.DEB.2.02.1112270633320.4050@pc7.dolda2000.com> <CALwzidmTdN5stuFfOuLPZ4RqNYihjJZMzy9NPAmEZhWOE_Ep0g@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4144.1325009109.27778.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On Tue, 27 Dec 2011, Ian Kelly wrote:
> On Mon, Dec 26, 2011 at 10:48 PM, Fredrik Tolf <fredrik@dolda2000.com> wrote:
>> I'm also a bit confused about __new__. I'd very much appreciate it if
>> someone could explain the following aspects of it:
>>
>>  * The manual (<http://docs.python.org/reference/datamodel.html>) says
>>   that __new__ is "a static method (special-cased so you need not declare
>>   it as such)". What does "special-cased" mean? Apparently, for
>>   instance, in OP's case,  Python did not automatically detect that it
>>   should not be bound as a method.
>
> It apparently has to do with the class creation.  For the
> special-casing to happen, the __new__ method has to be present when
> the class is created.  If it is, then it automatically gets wrapped in
> a staticmethod.  In the OP's case, he was adding the __new__ method
> after class creation, so the wrapping did not happen automatically.

Hmm, thank you. After trying a couple of things, it appears that the 
reason OP's method worked in Python 3 is that __get__ on ordinary 
functions in Python 3 simply returns the function itself when called on 
type lookup, rather than Python 2's unbound method objects.

> http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy
> """
> Class Types
>    Class types, or “new-style classes,” are callable. These objects
> normally act as factories for new instances of themselves, but
> variations are possible for class types that override __new__(). The
> arguments of the call are passed to __new__() and, in the typical
> case, to __init__() to initialize the new instance.
> """
>
> AFAIK, that's pretty much it.  When a type is called, __new__ is
> called to create the new instance, and then __init__ is called to
> initialize it (if __new__ returned an instance of the type).

Since that description doesn't include when __dict__ is created, it isn't 
complete, however. I guess it's safe to assume that __dict__ is created 
inside object.__new__, but that also leaves some things unclear; see 
below.

>> how methods, properties and the like are bound;
>
> When they're accessed, using the descriptor protocol, not as part of
> the instantiation process.  See:
> http://docs.python.org/reference/datamodel.html#invoking-descriptors

Ah, sorry. I had read that, but I had mistakenly thought that the same 
instance method object was always returned, which would have implied that 
it had to be stored for the instance somewhere, at some point. I see now 
that that is not actually the case, however. That clears up a whole lot 
for me. Thanks!

>> when and whether __dict__
>>   is created and a couple of other things.
>
> Under the hood as part of the object creation process, unless the
> class uses __slots__.

And also unless the object created is of type `object' or any other 
built-in type, which leaves me wondering exactly under what circumstances 
__dict__ actually is created. Is it some kind of special case in 
object.__new__ for types created with the `class' statement? There also 
appear to be other special cases in object.__new__:

>>> object.__new__(dict)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: object.__new__(dict) is not safe, use dict.__new__()

I guess my question reduces into the following: What does object.__new__ 
actually do, and what other special cases does it implement?

--

Fredrik Tolf

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

confused about __new__ "K. Richard Pixley" <rich@noir.com> - 2011-12-26 20:28 -0800
  Re: confused about __new__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-27 04:53 +0000
    Re: confused about __new__ K Richard Pixley <rich@noir.com> - 2011-12-26 21:27 -0800
  Re: confused about __new__ Fredrik Tolf <fredrik@dolda2000.com> - 2011-12-27 06:48 +0100
    Re: confused about __new__ K Richard Pixley <rich@noir.com> - 2011-12-27 09:41 -0800
      Re: confused about __new__ Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-27 11:28 -0700
      Re: confused about __new__ Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-27 13:34 -0700
        Re: confused about __new__ K Richard Pixley <rich@noir.com> - 2011-12-27 14:19 -0800
          Re: confused about __new__ Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-27 15:43 -0700
      Re: confused about __new__ K Richard Pixley <rich@noir.com> - 2011-12-27 12:31 -0800
  Re: confused about __new__ Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-27 00:28 -0700
  Re: confused about __new__ Lie Ryan <lie.1296@gmail.com> - 2011-12-27 18:47 +1100
  Re: confused about __new__ Fredrik Tolf <fredrik@dolda2000.com> - 2011-12-27 19:05 +0100

csiph-web