[Python-Dev] __doc__ behavior in class definitions

Jason Orendorff jason.orendorff at gmail.com
Sat Oct 8 00:51:21 CEST 2005


Martin,

These two cases generate different bytecode.

    def foo():     # foo.func_code.co_flags == 0x43
        print x    # LOAD_FAST 0
        x = 3

    class Foo:     # <code object>.co_flags == 0x40
        print x    # LOAD_NAME 'x'
        x = 3

In functions, local variables are just numbered slots. (co_flags bits
1 and 2 indicate this.)  The LOAD_FAST opcode is used.  If the slot is
empty, LOAD_FAST throws.

In other code, the local variables are actually stored in a
dictionary.  LOAD_NAME is used.  This does a locals dictionary lookup;
failing that, it falls back on the globals dictionary; and failing
that, it falls back on builtins.

Why the discrepancy?  Beats me.  I would definitely implement what
CPython does up to this point, if that's your question.

Btw, functions that use 'exec' are in their own category way out
there:

    def foo2():     # foo2.func_code.co_flags == 0x42
        print x     # LOAD_NAME 'x'
        exec "x=3"  # don't ever do this, it screws everything up
        print x

Pretty weird.  Jython seems to implement this.

-j


More information about the Python-Dev mailing list