in Python exec and eval statements, there's a scoping issue that will bite you in certain cases, one of which is a list comprehension. you'll get a NameError for a nonexistent variable that you know damned well is there. the way to avoid it is to pass a single environment containing everything in both globals() and locals(). in Python3.5 or later you can use exec(display_op, {**globals(), **locals()}), but in earlier versions the most concise way (not necessarily the most efficient) is exec(display_op, dict(list(globals().items()) + list(locals().items())).

I'm working on a Python implementation of the entire Bitcoin protocol, but right now I'm working on script, the FORTH-like stack language built in to process transactions. using exec is non-ideal but it simplifies things for now while I'm experimenting. I made some progress last night and I'm stoked.

Back to blog or home page

last updated 2018-01-14 12:11:22. served from tektonic.jcomeau.com