在Objective-C中,可以使用NSDictionaryOfVariableBindings
宏来创建这样的字典
NSString *foo = @"bar"
NSString *flip = @"rar"
NSDictionary *d = NSDictionaryOfVariableBindings(foo, flip)
// d -> { 'foo' => 'bar', 'flip' => 'rar' }
python中有类似的东西吗?我经常发现自己在写这样的代码
^{pr2}$做这样的事有捷径吗?在
d = dict(foo, flip) # -> {'foo': 'bar', 'flip': 'rar'}
你试过^{}
所以
Python没有办法做到这一点,尽管它有函数},它们可以让您访问整个本地或全局命名空间。但是如果您想选择所选的变量,我认为最好使用
locals
和{inspect
。这里有一个函数可以为您做到这一点:确保检查它在您使用的任何Python环境中都能正常工作。用法如下:
^{pr2}$不过,我不认为没有任何方法可以在变量名周围加引号。在
不,python中不存在此快捷方式。在
但也许这就是你需要的:
另外,python为这类事情提供了}的内置函数。
参见doc。在
globals()
和{相关问题 更多 >
编程相关推荐