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.