Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57214
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <mszamot@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'argument': 0.05; 'interpreter': 0.05; "'',": 0.07; 'class,': 0.07; 'debugging': 0.07; 'correspond': 0.09; 'namespace': 0.09; 'url:github': 0.09; 'way:': 0.09; 'cc:addr:python-list': 0.11; '(class': 0.16; '(starting': 0.16; '(when': 0.16; '__new__': 0.16; 'bases,': 0.16; 'called,': 0.16; 'metaclass': 0.16; 'namespace.': 0.16; 'subclass': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; '(but': 0.19; 'stack': 0.19; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; '(a)': 0.24; 'body,': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'point': 0.28; 'function': 0.29; '(this': 0.29; 'skip:( 20': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; '350': 0.31; 'inspect': 0.31; 'python2.7': 0.31; 'class': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'classes': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'keyword': 0.36; 'url:listinfo': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'level': 0.37; 'does': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'how': 0.40; 'hope': 0.61; 'first': 0.61; 'content-disposition:inline': 0.62; 'side': 0.67; 'insight': 0.68; 'sound': 0.68; 'evaluate': 0.72; 'hoping': 0.75; 'pipeline': 0.91; 'subject:Class': 0.91; 'to:none': 0.92 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=9fOxDfs9ZBEN0o/MEhKS2RbW3mId2CHBepXs/KRDYjE=; b=Wjo9KDgYCMvgkYeXaQ4LhtddESdiSPuMlQ9ZwQ0uGMm1UPnT7sD9bigQX0kn9fzXKs JLATiO/G2eGd58L+IVr8nsxahFSDJ2WTVfZxX+NVuABuuBjFkSWCdMMAwU3ohjItMoPe SxrCQ5ILGvkEMNTZQVOPwI5Hmd+BCZ5IVxtsrOgNAq5xhwAyat/WnrNzoLepnSZ4iMLP F9bOJvTWVhbPFeuAoesTIxKNwekb4XddkOsljLeyzigoMdsdOz00yXuT7z8qXV60iYVL EmR+Va4KRmEkpsd5ZtZKaBoYY/6n662W7yJ7Sj/CAx3KEdvrypj+v2kjZbDmsel9ri9X mTKg== |
| X-Received | by 10.180.13.210 with SMTP id j18mr11693123wic.51.1382396264253; Mon, 21 Oct 2013 15:57:44 -0700 (PDT) |
| Date | Mon, 21 Oct 2013 23:57:41 +0100 |
| From | Marcin Szamotulski <mszamot@gmail.com> |
| Cc | Python <python-list@python.org> |
| Subject | Re: Class construction |
| References | <CAE+T62bxxkVbfd9Kc4BQ7NEYOJYQB2v0MYO0njXBojEfv4w_SQ@mail.gmail.com> |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Disposition | inline |
| In-Reply-To | <CAE+T62bxxkVbfd9Kc4BQ7NEYOJYQB2v0MYO0njXBojEfv4w_SQ@mail.gmail.com> |
| User-Agent | Mutt/1.5.21 (2010-09-15) |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1328.1382396266.18130.python-list@python.org> (permalink) |
| Lines | 72 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1382396266 news.xs4all.nl 15906 [2001:888:2000:d::a6]:53924 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:57214 |
Show key headers only | View raw
You can inspect the process in this way:
>>> c = 'class A: pass'
>>> code = compile(c, '<stdin>', 'exec')
>>> from dis import dis
>>> dis(code)
1 0 LOAD_BUILD_CLASS
1 LOAD_CONST 0 (<code object A at 0x7effeef1c300, file "<stdin>", line 1>)
4 LOAD_CONST 1 ('A')
7 MAKE_FUNCTION 0
10 LOAD_CONST 1 ('A')
13 CALL_FUNCTION 2 (2 positional, 0 keyword pair)
16 STORE_NAME 0 (A)
19 LOAD_CONST 2 (None)
22 RETURN_VALUE
(this is in Python3.3, in Python2.7 the result will be different)
Now you can check ceval.c (PyEval_EvalFrameEx function) what are the
above opcode's doing. For example LOAD_BUILD_CLASS pushes the buildin
function builtins.__build_class__ to the stack and then CALL_FUNCTION
calls it. the c implementation of __build_class__ is in
Python/bltinmodule.c (builtin___build_class__). Indeed this function
will call the metaclass->tb_new `method` (but before it will also call
the __prepare__ method on metaclass to get the namespace). The first
argument on builtin__build_class__ is a function object which correspond
to the class body, and it is evaluated in the class namespace.
So the process (with some simplifications) goes like this:
1. get metaclass: meta
2. call meta.__prepare__ to get namespace (class __dict__)
3. evaluate the class body in the namespace
4. call meta with arguemnts: name, bases, namespace
(when you instantiate any class in Python, metaclass is just
a class, first its __new__ method is called, and then its __init__)
All this is in builtin__build_class__.
I hope it helps,
Regards,
Marcin
On 08:08 Mon 21 Oct , Demian Brecht wrote:
> Hi all,
>
> I'm trying to wrap my head around how classes are constructed at the
> interpreter level (as a side effect of digging into metaclasses) and
> I'm hoping to have my investigation either validated or ridiculed ;)
>
> The pipeline that I've figured through some gdb debugging (starting at
> type_call):
>
> + type_call
> + is there a metaclass for this object?
> + return metaclass->tp_new
> + roughly 350 LOC constructing the type
> + is object is not a subclass of type?
> + return object
> + call obj->tp_init
>
> Does that sound correct? My C/gdb skills are horribly rusty at this
> point so expert insight would help :)
>
> Thanks,
>
> --
>
> Demian Brecht
> http://demianbrecht.github.com
> --
> https://mail.python.org/mailman/listinfo/python-list
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Class construction Marcin Szamotulski <mszamot@gmail.com> - 2013-10-21 23:57 +0100 Re: Class construction Neil Cerutti <neilc@norwich.edu> - 2013-10-22 15:48 +0000
csiph-web