locals

Is there any way to affect locals at runtime?

假如想象 提交于 2020-01-16 09:11:34
问题 I actually want to create a new local. I know it sounds dubious, but I think I have a nice use case for this. Essentially my problem is that this code throws "NameError: global name 'eggs' is not defined" when I try to print eggs: def f(): import inspect frame_who_called = inspect.stack()[1][0] frame_who_called.f_locals['eggs'] = 123 def g(): f() print(eggs) g() I found this old thing: http://mail.python.org/pipermail/python-dev/2005-January/051018.html Which would mean I might be able to do

How to dynamically modify a function's local namespace?

不打扰是莪最后的温柔 提交于 2020-01-11 06:00:26
问题 NB: This question assumes Python 2.7.3. I'm looking for a sane approach to dynamically modify a function's local namespace, preferably in a way that adds the least clutter to the body function. What I have in mind would look something like this: import os from namespace_updater import update_locals def somefunc(x, y, z): # ... # ... # this and that # ... # ... if os.environ.get('FROBNICATE'): from frobnitz import frobnicate update_locals(frobnicate(locals())) # # life goes on, possibly with

How to dynamically modify a function's local namespace?

我只是一个虾纸丫 提交于 2020-01-11 06:00:25
问题 NB: This question assumes Python 2.7.3. I'm looking for a sane approach to dynamically modify a function's local namespace, preferably in a way that adds the least clutter to the body function. What I have in mind would look something like this: import os from namespace_updater import update_locals def somefunc(x, y, z): # ... # ... # this and that # ... # ... if os.environ.get('FROBNICATE'): from frobnitz import frobnicate update_locals(frobnicate(locals())) # # life goes on, possibly with

Python scoping in dict comprehension

人盡茶涼 提交于 2019-12-30 08:18:34
问题 >>> x = 'foo' >>> {0: locals().get('x')} {0: 'foo'} >>> {0: locals().get('x' + spam) for spam in ['']} {0: None} What is the reason for this discrepancy in behaviour? 回答1: Dict comprehensions and generator comprehensions create their own local scope. List comprehensions do not in Python 2.x, but do in Python 3. (Note that your first example is not a dict comprehension. It's just a literal dict that happens to have an expression as the value for the key 0.) 来源: https://stackoverflow.com

Getting local variables from a stack frame on the JVM

风格不统一 提交于 2019-12-22 03:51:07
问题 Is there any way to get a map or other data structure of the local variables in the current scope in on the JVM without using a debugger? That is, to get the locals of the current stack frame? I know that there are stacktrace objects, but StackTraceElement has no way to get access to any state. It just tells you what method was called where, but not what was in it. 回答1: Variable names can be included in class files to aid debuggers, but javac doesn't do it by default. It requires the -g:vars

locals().update(dictionary) doesn't add all the variables

北慕城南 提交于 2019-12-18 17:37:36
问题 I have been loading variables using dictionary objects, but the values get updated. What am I missing here? assert "run_LMM" in all_variables.keys() locals().update(all_variables) assert "run_LMM" in locals() The last line is were I get an assertion error. What's going on? 回答1: That's the expected behaviour, by the docs: The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter. I think, one of the reasons for

Alternative to locals() in printing a table with a header

倾然丶 夕夏残阳落幕 提交于 2019-12-11 12:35:00
问题 [Python 3.1] Edit: mistake in the original code. I need to print a table. The first row should be a header, which consists of column names separated by tabs. The following rows should contain the data (also tab-separated). To clarify, let's say I have columns "speed", "power", "weight". I originally wrote the following code, with the help from a related question I asked earlier: column_names = ['speed', 'power', 'weight'] def f(row_number): # some calculations here to populate variables speed

Getting local variables from a stack frame on the JVM

大憨熊 提交于 2019-12-05 01:28:37
Is there any way to get a map or other data structure of the local variables in the current scope in on the JVM without using a debugger? That is, to get the locals of the current stack frame? I know that there are stacktrace objects, but StackTraceElement has no way to get access to any state. It just tells you what method was called where, but not what was in it. erickson Variable names can be included in class files to aid debuggers, but javac doesn't do it by default. It requires the -g:vars option. If it's present, a program could use a byte-code engineering library like ASM to access the

Rails 3, passing local variable to partial [duplicate]

為{幸葍}努か 提交于 2019-12-03 06:44:12
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Rails: confused about syntax for passing locals to partials I want to pass local variable(which doesn't have relevant field in model) to partial. # infos/index.html.erb <%= render :partial => 'info', :locals => {:info => first, :img_style => "original"} %> :img_style will be html style for image. # infos/_info.html.erb <% first = @infos.shift %> <%= image_tag(info.image.url, :class => img_style), info %> # and