Re-executing the code object from a class 'declaration'

Alex Martelli aleaxit at yahoo.com
Wed Oct 6 22:38:40 CEST 2004


Carlos Ribeiro <carribeiro at gmail.com> wrote:
   ...
> > Nope, f doesn't get the bases AT ALL; the bases (as a tuple) go in only
> > when you call the metaclass.
> 
> Well. I was mistaken (and that's not be the first time). It goes like
> this; I first checked the dis.dis() code:
> 
>  27          64 LOAD_CONST               4 ('MyClass')
>              67 LOAD_NAME                7 (object)
>              70 BUILD_TUPLE              1
>              73 LOAD_CONST               5 (<code object MyClass at
> 0120A0A0, file "c:\work\help-on-c-l-py\re_exec_class.py", line 27>)
>              76 MAKE_FUNCTION            0
>              79 CALL_FUNCTION            0
>              82 BUILD_CLASS         
>              83 STORE_NAME               8 (MyClass)
> 
> ... with that build_tuple there... besides that I knew I *can* access
> the bases from within my class statement body:
> 
> class MyClass(MyBaseClass):
>     bar = MyBaseClass.foo
>  
> ... so I had suspected that the bases *were* passed to f(). But wait!
> Actually, Python is building the stack for the BUILD_CLASS opcode.
> Right?

Right.  When you mention MyBaseClass in the body, it gets looked up in
the globals, just where it was looked up when you passed it as a base.
The 0 in MAKE_FUNCTION _and_ CALL_FUNCTION opcodes refers to the number
of arguments to the function, btw...


Alex



More information about the Python-list mailing list