Sujet : Re: Is Lexical Binding The Norm Yet?
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 21. Jan 2024, 18:37:51
Autres entêtes
Organisation : Stefan Ram
Message-ID : <binding-20240121183646@ram.dialup.fu-berlin.de>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[a multipost, as an exception]
ram@zedat.fu-berlin.de (Stefan Ram) writes:
the Python standard library assumes class-based objects.)
Since the thread is about "binding": Python can be very
explicit when it comes to binding (should you need it).
main.py
import types
x = 2
def f():
return x
# prints "2"
print( f() )
g = types.FunctionType( f.__code__, { 'x': 4 } )
# prints "4"
print( g() )
h = types.FunctionType( f.__code__, globals() )
# prints "2"
print( h() )
def a():
x = 3
# prints "2"
print( f() )
# prints "3"
print( types.FunctionType( f.__code__, locals() )() )
# prints "2"
print( types.FunctionType( f.__code__, globals() )() )
a()