获取当前变量名称 [英] Getting current variable name

查看:100
本文介绍了获取当前变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在2005年3月的帖子中跟踪了题为如何将变量名变成

字符串?的邮件,因为我有类似的问题。


我必须在我的程序中获得一些列表变量名称。所以

我最终调查了globals()来获得一个像

这样的小函数:


#!/ usr / bin / python

l1 = [''r'',''r'','t'']

l2 = [' 'r'','t'','t'']

l3 = [''t'','t'','t'']#两个等效列表但是......

l4 = [''t'','t'','t'']#用不同的名字


def nameofObj(obj):

#print locals()

globdict = globals()

var = globals()。keys()

for i in var:

if globdict [i] == obj:

print i

print'' - ''* 20,''\ n''

nameofObj(l1)


print'' - ''* 20,''\ n ''

map(nameofObj,[l1,l2,l3,l4])


-------------- -------------------------------------------------- ----

如果你试试这个,有2个等价的名单

(这里l3和l4当使用map时,该函数将返回

可能性l3和l4我理解。

因此这个解决方案仅限于唯一列表,不幸的是我确实有

等价列表在我的情况下...


问题来自于locals()和globals()有

共同的价值,但不是那些无法测试的键。


我看到的唯一解决方案是链接列表及其名称(在
$中)例如b $ ba字典)但这对我来说听起来并不优雅?

如果你们可以帮助一个新手...


谢谢你们/>
-

---------------------------------- --------------------------------

| Patrick LADAM | |

| Laboratoire CSSB |大爆炸理论:|

| UFR SMBH | |

| 74 rue Marcel Cachin |在开始时有|

| 93017 Bobigny CEDEX |什么都没有。 |

| >>>新电子邮件:<<< | |

| la***@smbh.smbh.univ-paris13.fr |然后,它爆炸了...... |

|电话:01 48 38 77 26/76 85 | |

|传真:01 48 38 77 77 | |

----------------------------------------- -------------------------

Hi all,
I followed the mails entitled ''How to turn a variable name into a
string?'' in march 2005 posts as I have a similar problem.

I have to get some list variable names at some point in my program. So
I ended up looking into globals() to get them with a small function like
this:

#!/usr/bin/python

l1 = [''r'', ''r'', ''t'']
l2 = [''r'', ''t'', ''t'']
l3 = [''t'', ''t'', ''t''] # Two equivalent lists but...
l4 = [''t'', ''t'', ''t''] # with different names

def nameofObj(obj):
# print locals()
globdict = globals()
var = globals().keys()
for i in var :
if globdict[i] == obj:
print i
print ''-''*20 ,''\n''
nameofObj(l1)

print ''-''*20 ,''\n''
map(nameofObj, [l1, l2, l3, l4])

--------------------------------------------------------------------
If you try this, with 2 equivalent lists
(here l3 and l4 when using map) the function will return both
possibilities l3 and l4 which I understand.
So this solution is limitated to unique lists, unfortunately I do have
equivalent lists in my case...

The problem comes from the fact that locals() and globals() have
in common their values but not the keys which then can''t be tested.

The only solution I see would be to link the list and its name (in
a dictionary for instance) but that does not sound elegant to me?
If you guys can help a newbie...

Thank you
--
------------------------------------------------------------------
| Patrick LADAM | |
| Laboratoire CSSB | THE BIG BANG THEORY: |
| UFR SMBH | |
| 74 rue Marcel Cachin | In the begining there was |
| 93017 Bobigny CEDEX | nothing at all. |
| >>> NEW e-mail: <<< | |
| la***@smbh.smbh.univ-paris13.fr | Then, it exploded... |
| Tel: 01 48 38 77 26 / 76 85 | |
| Fax: 01 48 38 77 77 | |
------------------------------------------------------------------


推荐答案

pl写道:
我必须在程序中的某个位置获取一些列表变量名称。所以
我最终调查了globals()来获得一个像
这样的小函数:
[...] var = globals()。keys()
for我在var:
如果globdict [i] == obj:
打印我
I have to get some list variable names at some point in my program. So
I ended up looking into globals() to get them with a small function like
this: [...] var = globals().keys()
for i in var :
if globdict[i] == obj:
print i




使用''是''而不是''= = ''。如果参数是

相同的对象,则返回true:



Use ''is'' instead of ''==''. This will return true if the arguments are the
same object:

l1 = [1,2 ,3]
l2 = [1,2,3]
l1 == l2
真l1是l2
l1 = [1, 2, 3]
l2 = [1, 2, 3]
l1 == l2 True l1 is l2



False


Daniel


False

Daniel


pl写道:
我跟着邮件标题为''如何在2005年3月的帖子中将变量名转换为
字符串?''因为我有类似的问题。

我必须在我的程序中的某些时候获得一些列表变量名。所以
我最后调查了globals()来获得一个像
这样的小函数:

#!/ usr / bin / python
l1 = [''r'',''r'','t'']
l2 = [''r'',''t'','t''] l3 = [''t'',''t'','t'']#两个等效的列表,但......
l4 = [''t'',''t'',' 't'']#具有不同的名称

def nameofObj(obj):
#print locals()
globdict = globals()
var = globals() .keys()
我在var:
如果globdict [i] == obj:
打印我

打印'' - ''* 20,' '\ n''
nameofObj(l1)

打印'' - ''* 20,''\ n''
地图(nameofObj,[l1,l2 ,l3,l4])
I followed the mails entitled ''How to turn a variable name into a
string?'' in march 2005 posts as I have a similar problem.

I have to get some list variable names at some point in my program. So
I ended up looking into globals() to get them with a small function like
this:

#!/usr/bin/python

l1 = [''r'', ''r'', ''t'']
l2 = [''r'', ''t'', ''t'']
l3 = [''t'', ''t'', ''t''] # Two equivalent lists but...
l4 = [''t'', ''t'', ''t''] # with different names

def nameofObj(obj):
# print locals()
globdict = globals()
var = globals().keys()
for i in var :
if globdict[i] == obj:
print i
print ''-''*20 ,''\n''
nameofObj(l1)

print ''-''*20 ,''\n''
map(nameofObj, [l1, l2, l3, l4])




你在这里试图解决的问题是什么?查找对象的名称

通常不是你想做的事情。如果您在用例上提供更多详细信息,我们可能会帮助您

重构此代码。


STeVe



What is the problem you''re trying to solve here? Looking up the names
of an object is not usually something you want to do. If you provide a
little more detail on your use case, we might be able to help you
refactor this code.

STeVe


pl写道:
大家好,我跟着标题为''如何将变量名变成a
字符串?''在2005年3月发布,因为我有类似的问题。
Hi all,
I followed the mails entitled ''How to turn a variable name into a
string?'' in march 2005 posts as I have a similar problem.




使用locals()函数而不是globals()。


顺便说一下,我想知道如何做到这一点,你的帖子,以及

Daniel指出''是'',帮我解决了这个问题。应该有一个更简单的方法,不需要踩到名单。


这不会以同样的方式使用列表,但我认为它回答了你的问题。


def getvinfo(vars,v):

"""

vars是当地人()

v是[varable]

使用一个项目列表通过引用传递单个varables。
$对于vars.keys()中的n,b $ b"""


如果vars [n]为v [0],则为


返回n,v [0],输入(v [0])


a = 101

b = 2.3

c =真


打印getvinfo(本地人(),[a])

打印getvinfo(本地人(),[b])

print getvinfo(locals(),[c])



Use the locals() function instead of globals().

Thanks by the way, I was wondering how to do this also, your post, and
Daniel pointing out ''is'', helped me work this out. There should be an
easier way that doesn''t require stepping though the name list.

This doesn''t use lists in the same way, but I think it answers your
question.

def getvinfo(vars, v):
"""
vars is locals()
v is [varable]
Use an one item list to pass single varables by reference.
"""
for n in vars.keys():
if vars[n] is v[0]:
return n, v[0], type(v[0])

a = 101
b = 2.3
c = True

print getvinfo(locals(), [a])
print getvinfo(locals(), [b])
print getvinfo(locals(), [c])



('''',101,< type''int''>)

(''b'',2.2999999999999998,<输入''fl oat''>)

(''c'',True,< type''bool''>)

这对打印错误消息很有用和调试信息。


Ronald Adam


(''a'', 101, <type ''int''>)
(''b'', 2.2999999999999998, <type ''float''>)
(''c'', True, <type ''bool''>)
This could be useful for printing error messages and debugging info.

Ronald Adam


这篇关于获取当前变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆