Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18015
| 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 | <ian.g.kelly@gmail.com> |
| 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 | <alpine.DEB.2.02.1112270633320.4050@pc7.dolda2000.com> |
| References | <MPbKq.47607$cN1.25052@newsfe12.iad> <alpine.DEB.2.02.1112270633320.4050@pc7.dolda2000.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Tue, 27 Dec 2011 00:28:24 -0700 |
| Subject | Re: confused about __new__ |
| To | Fredrik Tolf <fredrik@dolda2000.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4128.1324970938.27778.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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.
Compare:
>>> def my_new(cls): return object.__new__(cls)
...
>>> class Foo(object):
... __new__ = my_new
...
>>> class Bar(object): pass
...
>>> Bar.__new__ = my_new
>>> Foo.__dict__['__new__']
<staticmethod object at 0x0237D6F0>
>>> Bar.__dict__['__new__']
<function my_new at 0x02381430>
> * Is there any part of the manual that explains, holistically, the
> greater context of object instantiation into which __new__ fits? I can
> only find small parts of it spread around the documentation for __new__
> and __init__, but no complete explanation of it. There are several
> things I'm wondering about, like what it means to call a type object at
> 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 “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).
> 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
> 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__
> is created and a couple of other things.
Under the hood as part of the object creation process, unless the
class uses __slots__.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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