我想在函数外部使用函数中定义的一堆局部变量.所以我传递x=locals()
了返回值.
如何将该字典中定义的所有变量加载到函数外部的命名空间中,这样x['variable']
我就可以简单地使用,而不是使用值来访问该值variable
.
关于使用以下模式是否有任何缺点,警告或不良做法警告?
def buildString(user, name = 'john', age=22):
userId = user.getUserId()
return "Name: {name}, age: {age}, userid:{userId}".format(**locals())
Run Code Online (Sandbox Code Playgroud)
我有一个非常重复的字符串生成代码来编写并且很想使用它,但是关于使用的东西locals()
让我感到不舒服.这有意外行为的危险吗?
编辑:上下文
我发现自己经常写下这样的东西:
"{name} {age} {userId} {etc}...".format(name=name, age=age, userId=userId, etc=etc)
Run Code Online (Sandbox Code Playgroud) 我想将局部变量(在模型中没有相关字段)传递给partial.
# infos/index.html.erb
<%= render :partial => 'info', :locals => {:info => first, :img_style => "original"} %>
Run Code Online (Sandbox Code Playgroud)
:img_style将是图像的html样式.
# infos/_info.html.erb
<% first = @infos.shift %>
<%= image_tag(info.image.url, :class => img_style), info %>
# and here goes code for normal iteration
<% @infos.each do |e| %>
# etc
Run Code Online (Sandbox Code Playgroud)
但它不起作用,它返回错误:
# GET /infos/
undefined local variable or method `img_style' for #<#<Class:0xc471f34>:0xc470cc4>
Run Code Online (Sandbox Code Playgroud)
它可以在不做多余部分的情况下完成吗?
对不起我的英语不好.:P
编辑:
井模型信息没有:img_style字段
# db/schema.rb
create_table "infos", :force => true do |t|
t.string "title"
t.text "description"
t.integer "place_id" …
Run Code Online (Sandbox Code Playgroud) 我的一个Debug.Assert()失败,所以我得到一个带调用堆栈的窗口,然后单击重试.此时,在"局部"窗口中,"值"列中的某些行具有红色文本而不是黑色文本.这是什么意思?
所以我使用locals()来获取函数中的一些参数.很好地工作:
def my_function(a, b):
print locals().values()
>>> my_function(1,2)
[1, 2]
Run Code Online (Sandbox Code Playgroud)
标准的东西.但现在让我们介绍一下列表理解:
def my_function(a, b):
print [x for x in locals().values()]
>>> my_function(1,2)
[[...], 1, 2]
Run Code Online (Sandbox Code Playgroud)
EHH?为什么要插入自引用?
python list-comprehension python-2.6 locals python-internals
有没有办法在不使用调试器的情况下在JVM上获取当前作用域中的局部变量的映射或其他数据结构?也就是说,获取当前堆栈帧的本地?
我知道有堆栈跟踪对象,但StackTraceElement
无法访问任何状态.它只是告诉你在哪里调用了哪种方法,而不是它里面的内容.
有人可能知道如何 在visual studio 2010中搜索本地的值,或者至少如何扩展所有节点,子节点?
所有,
我遇到了标准fields_for设置的问题.在我的"_form"部分我有:
<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>
Run Code Online (Sandbox Code Playgroud)
在"_comment_fields"部分中,我有通常的字段,然后是我的测试变量:
<%= tester.to_s %>
Run Code Online (Sandbox Code Playgroud)
当我删除测试器变量时,一切都运行良好.一旦我添加测试变量,我就会收到此错误:
ActionView :: Template :: Error(#Class的未定义局部变量或方法`tester':0xa1f3664>:0xa1f1bd4>)
有没有其他人在使用具有多个本地的fields_for时遇到此问题?
为了详细说明,我的"_comment_fields"部分看起来像这样:
<div class="comment dynamic_field">
<span class="comment_content"><%= f.text_field :content, :class => "comment_content" %></span>
<%= tester.to_s %>
<%= link_to_remove_fields "remove", f %>
</div>
Run Code Online (Sandbox Code Playgroud)
它仅从"_form"部分调用.
>>> x = 'foo'
>>> {0: locals().get('x')}
{0: 'foo'}
>>> {0: locals().get('x' + spam) for spam in ['']}
{0: None}
Run Code Online (Sandbox Code Playgroud)
这种行为差异的原因是什么?