locals

Viewing an object in Locals or Watch window causes excel to crash

青春壹個敷衍的年華 提交于 2019-12-01 20:20:39
问题 In Excel when I'm running some code and put a breakpoint in I can look at the values of things in the locals window. In the locals window, when I try to expand a object for the class I've created Excel Crashes with "Microsoft Office Excel has encountered a problem and needs to close. We are sorry for the inconvinience. This also happens if I try to view the object in the watch window. Any ideas? Or anyone had this before? Thanks, Chris 回答1: Check, check again and recheck your class properties

How to dynamically modify a function's local namespace?

喜你入骨 提交于 2019-12-01 06:56:17
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 duly frobnicated local variables... # ... # ... # ... Thanks! PS: Below are approaches that don't work.

Python scoping in dict comprehension

只谈情不闲聊 提交于 2019-12-01 03:37:20
>>> 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? 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/questions/13117020/python-scoping-in-dict-comprehension

Using locals() and format() method for strings: are there any caveats?

醉酒当歌 提交于 2019-11-30 04:11:42
Are there any disadvantages, caveats or bad practice warnings about using the following pattern? def buildString(user, name = 'john', age=22): userId = user.getUserId() return "Name: {name}, age: {age}, userid:{userId}".format(**locals()) I had a very repetitive string generation code to write and was tempted to use this, but something about using locals() makes me uncomfortable. Is there any danger of unexpected behavior in this? Edit: context I found myself constantly writing stuff like: "{name} {age} {userId} {etc}...".format(name=name, age=age, userId=userId, etc=etc) There is now an

Using locals() and format() method for strings: are there any caveats?

余生长醉 提交于 2019-11-29 02:04:29
问题 Are there any disadvantages, caveats or bad practice warnings about using the following pattern? def buildString(user, name = 'john', age=22): userId = user.getUserId() return "Name: {name}, age: {age}, userid:{userId}".format(**locals()) I had a very repetitive string generation code to write and was tempted to use this, but something about using locals() makes me uncomfortable. Is there any danger of unexpected behavior in this? Edit: context I found myself constantly writing stuff like: "

rails fields_for render partial with multiple locals producing undefined variable

一笑奈何 提交于 2019-11-27 16:21:59
问题 All, I am experiencing a problem with a standard fields_for setup. In my "_form" partial I have: <div class="comment_list"> <%= f.fields_for :comments do |cf| %> <%= render :partial => 'comments/comment_fields', :locals => {:f => cf, :tester => true} %> <% end %> <%= link_to_add_fields "Add a comment", f, :comments %> </div> In the "_comment_fields" partial, I have the usual fields and then my test variable: <%= tester.to_s %> When I remove the tester variable, everything works well. As soon

Python: load variables in a dict into namespace

好久不见. 提交于 2019-11-26 21:53:39
I want to use a bunch of local variables defined in a function, outside of the function. So I am passing x=locals() in the return value. How can I load all the variables defined in that dictionary into the namespace outside the function, so that instead of accessing the value using x['variable'] , I could simply use variable . Consider the Bunch alternative: class Bunch(object): def __init__(self, adict): self.__dict__.update(adict) so if you have a dictionary d and want to access (read) its values with the syntax x.foo instead of the clumsier d['foo'] , just do x = Bunch(d) this works both

python locals和globals

☆樱花仙子☆ 提交于 2019-11-26 19:24:03
locals和globals 标记一下:Dive Into Python 内容 我们先偏离一下 HTML 处理的主题, 讨论一下 Python 如何处理变量。 Python 有两个内置的函数,locals和globals, 它们提供了基于 dictionary 的访问局部和全局变量的方式。 还记得locals吗? 您第一次是在这里看到的: def unknown_starttag (self, tag, attrs): strattrs = "" .join([ ' %s="%s"' % (key, value) for key, value in attrs]) self.pieces.append( "<%(tag)s%(strattrs)s>" % locals()) 不, 等等, 此时您还不能理解locals。首先, 您需要学习关于命名空间的知识。这很枯燥, 但是很重要, 因此要要耐心些。 Python 使用叫做名字空间的东西来记录变量的轨迹。名字空间只是一个 dictionary ,它的键字就是变量名,它的值就是那些变量的值。实际上,名字空间可以象 Python 的 dictionary 一样进行访问,一会我们就会看到。 在一个 Python 程序中的任何一个地方,都存在几个可用的名字空间。每个函数都有着自已的名字空间,叫做局部名字空间,它记录了函数的变量

Why is it bad idea to modify locals in python?

落花浮王杯 提交于 2019-11-26 17:16:21
问题 Related to this reply here. Locals' doc here. The docs mention that the dictionary should not change, not sure what it means but would locals() be applicable in lab-reports where data won't change, for example in measurements? 回答1: Modification is a bad idea because the documentation (which you link) explicitly says not to: Note: The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter. You don't need any

Python: load variables in a dict into namespace

非 Y 不嫁゛ 提交于 2019-11-26 07:23:03
问题 I want to use a bunch of local variables defined in a function, outside of the function. So I am passing x=locals() in the return value. How can I load all the variables defined in that dictionary into the namespace outside the function, so that instead of accessing the value using x[\'variable\'] , I could simply use variable . 回答1: Consider the Bunch alternative: class Bunch(object): def __init__(self, adict): self.__dict__.update(adict) so if you have a dictionary d and want to access