Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'instance,': 0.05; '"""': 0.07; 'happen,': 0.07; 'initialize': 0.07; 'override': 0.07; 'see:': 0.07; 'callable.': 0.09; 'descriptor': 0.09; 'instance.': 0.09; 'variations': 0.09; 'things.': 0.12; 'def': 0.13; 'method.': 0.15; '__init__': 0.16; 'bound;': 0.16; 'called,': 0.16; 'creation,': 0.16; 'creation.': 0.16; 'explanation': 0.16; 'foo(object):': 0.16; 'hierarchy,': 0.16; 'instantiate': 0.16; 'methods,': 0.16; 'type:': 0.16; 'cc:addr:python-list': 0.16; 'mon,': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'arguments': 0.18; 'instance': 0.18; 'cc:no real name:2**0': 0.20; 'detect': 0.21; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.24; 'static': 0.24; 'says': 0.25; "i'm": 0.26; 'process,': 0.28; 'bit': 0.28; 'message-id:@mail.gmail.com': 0.28; 'pass': 0.29; 'skip:b 20': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'class': 0.29; 'confused': 0.30; 'url:library': 0.31; 'does': 0.32; 'wondering': 0.32; 'objects': 0.32; 'pretty': 0.32; 'instead': 0.33; 'there': 0.33; 'object': 0.33; 'it.': 0.34; 'someone': 0.34; 'normally': 0.34; 'received:209.85.212': 0.34; 'things': 0.34; 'calling': 0.34; 'anything': 0.34; 'themselves,': 0.34; 'typical': 0.34; 'unless': 0.35; 'url:python': 0.36; 'explain': 0.36; 'properties': 0.36; 'uses': 0.36; 'bound': 0.37; 'skip:" 10': 0.37; 'but': 0.37; 'passed': 0.37; 'reference': 0.37; 'received:google.com': 0.37; 'spread': 0.37; 'could': 0.37; 'using': 0.38; 'several': 0.38; 'received:209.85': 0.38; 'couple': 0.38; 'created': 0.38; 'returned': 0.39; 'url:docs': 0.39; "i'd": 0.39; 'url:org': 0.39; 'should': 0.39; 'called': 0.40; 'received:209': 0.40; 'type': 0.61; '2011': 0.61; 'types': 0.61; 'happen': 0.61; 'covers': 0.64; 'believe': 0.65; 'act': 0.65; '26,': 0.67; 'all;': 0.84; 'compare:': 0.84; 'fredrik': 0.84; 'hood': 0.84; 'url:datamodel': 0.84; 'url:html#object': 0.84; 'url:reference': 0.84; 'about,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=cjhpV1BaCC0wt2z46ECayGXt3faiw53202ukxThYX6Q=; b=Tgp2odOBYXavneJQrCYxkJANDdKf6/+mM3MPH4lu7YsMatHf66HDMgyt1eE+gsrcn+ WGD78wd9zrmMKG4XKNjITbUb0czxtFvlvxP+Ply6+8b8kUaYJXVYocSx8r7uB7g77YIl lu+kxjHT77VU3m0vYb50hxZqsyuYXNUXymHTw= MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Tue, 27 Dec 2011 00:28:24 -0700 Subject: Re: confused about __new__ To: Fredrik Tolf Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324970938 news.xs4all.nl 6854 [2001:888:2000:d::a6]:55845 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18015 On Mon, Dec 26, 2011 at 10:48 PM, Fredrik Tolf wrot= e: > I'm also a bit confused about __new__. I'd very much appreciate it if > someone could explain the following aspects of it: > > =A0* The manual () says > =A0 that __new__ is "a static method (special-cased so you need not decla= re > =A0 it as such)". What does "special-cased" mean? Apparently, for > =A0 instance, in OP's case, =A0Python did not automatically detect that i= t > =A0 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. Compare: >>> def my_new(cls): return object.__new__(cls) ... >>> class Foo(object): ... __new__ =3D my_new ... >>> class Bar(object): pass ... >>> Bar.__new__ =3D my_new >>> Foo.__dict__['__new__'] >>> Bar.__dict__['__new__'] > =A0* Is there any part of the manual that explains, holistically, the > =A0 greater context of object instantiation into which __new__ fits? I ca= n > =A0 only find small parts of it spread around the documentation for __new= __ > =A0 and __init__, but no complete explanation of it. There are several > =A0 things I'm wondering about, like what it means to call a type object = at > =A0 all; I don't know of anything that organizes it that way specifically, but I believe the Data Model reference pretty much covers what you're looking for. From the type hierarchy, under "Callable Types": http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy """ Class Types Class types, or =93new-style classes,=94 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). > 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 > how pickle can > =A0 instantiate a class without calling __init__; By calling the __new__ method directly instead of calling the type: http://docs.python.org/library/pickle.html#object.__getnewargs__ > when and whether __dict__ > =A0 is created and a couple of other things. Under the hood as part of the object creation process, unless the class uses __slots__.