如何将变量名称转换为字符串? [英] How to turn a variable name into a string?

查看:156
本文介绍了如何将变量名称转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做以下事情:


a = 1; b = 2; c =无

mylist = [a,b,c]
我的in mylist


如果我是无:

print''你有问题%s''%我的#这行是有问题的

I''d like to do something like the following:

a = 1; b = 2; c = None
mylist = [a, b, c]
for my in mylist:
if my is None:
print ''you have a problem with %s'' % my #this line is problematic

你有问题无


我想在输出中看到的是:你有问题c
You have a problem with None
What I want to see in the output is: You have a problem with c




如何将变量名称转换为字符串?


谢谢!


-

Stewart Midwinter
st*****@midwinter.ca
< a href =mailto:st *************** @ gmail.com> st *************** @ gmail.com

推荐答案

Stewart Midwinter写道:
Stewart Midwinter wrote:
我想做类似以下的事情:

a = 1; b = 2; c =无
mylist = [a,b,c]
我的mylist:
如果我是没有:
打印''你有问题%s''我的#this行是有问题的
I''d like to do something like the following:

a = 1; b = 2; c = None
mylist = [a, b, c]
for my in mylist:
if my is None:
print ''you have a problem with %s'' % my #this line is problematic
你遇到问题没有
我想在输出中看到的是:你有一个问题c
You have a problem with None
What I want to see in the output is:You have a problem with c



如何将变量名称转换为字符串?



How do I convert a variable name into a string?




你不能。在上面的例子中,当您点击列表中的无项目时,只有一个无

对象,并且有三个不同的名称

绑定它:c,我和

列表中的第三个位置。 (想想这个绑定的东西就好像

字符串将各种名字附加到物品上

本身。)


给定对象无那么,Python怎么知道你想要的名字是什么?b $ b更多的是

点,当你完成创建

列表mylist时,与c的连接是已经很久了。

就像你输入的那样:

mylist = [1,2,None]


毫无疑问,还有其他方法可以实现你真正想要做的事情(显示某些

信息用于调试目的吗?),但是用

人为的例子有点难以选择

最好...


顺便说一句,这类事情应该是好的

由FAQ条目覆盖,如果你阅读它们。

参见,例如,
http://www.python.org /doc/faq/progra...e-of-an-object


-Peter



You cannot. In the above example, when you hit
the None item in the list, there is only one None
object in existence, and three different names
"bound" to it: c, my, and the third spot in the
list. (Think of this "bound" thing as being like
strings attaching a variety of names to the item
itself.)

Given the object "None" then, how can Python know
which of the names you intended? More to the
point, by the time you''ve finished creating the
list mylist, the connection to "c" is long gone.
It is exactly as though you had typed this instead:
mylist = [1, 2, None]

There are doubtless other ways to accomplish what
you are really trying to do (display some sort of
information for debugging purposes?), but with a
contrived example it''s a little hard to pick the
best...

By the way, this sort of thing should be well
covered by the FAQ entries, if you read them.
See, for example,
http://www.python.org/doc/faq/progra...e-of-an-object

-Peter


任何给定的Python对象可能绑定多个名称或者根本不绑定,

所以试图找到引用一个对象的符号类似于
quixotic。


事实上,你''没有被我的和我的引用的和c在这个

的例子中,在一个更复杂的程序中,

几十个符号将引用它,因为它是唯一的[例如(a ==无,b ==无)

必需(a是b)== True]


您可以尝试使用is将它与命名空间中的某些内容相匹配,

虽然。这可能会做你想要的。


def名称(东西):

返回[name for name,ref in globals()。iteritems()if ref是的东西]


a = 1; b = 2; c =无


mylist = [a,b,c]
我的in mylist


如果我是无:

打印''你有问题%s''%姓名(我的)

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


虽然重写这样的代码要简单得多:


a = 1; b = 2; c =无


mylist = [''a'',''b'',''c'']

for my_name in mylist:

my = eval(my_name)

如果我是无:

打印''你有问题%s''%my_name

Any given Python object may be bound to multiple names or none at all,
so trying to find the symbol(s) which reference an object is sort of
quixotic.

In fact, you''ve got None referenced by both "my" and "c" in this
example, and in a more complicated program None will be referenced by
dozens symbols because it''s unique [e.g. (a == None, b == None)
necessitates (a is b) == True]

You could try using "is" to match it with something in the namespace,
though. This might do what you want.

def names(thing):
return [name for name,ref in globals().iteritems() if ref is thing]

a = 1; b = 2; c = None

mylist = [a,b,c]
for my in mylist:
if my is None:
print ''you have a problem with %s'' % names(my)
-------------

Although it would be much simpler to rewrite your code like this:

a = 1; b = 2; c = None

mylist = [''a'',''b'',''c'']
for my_name in mylist:
my = eval(my_name)
if my is None:
print ''you have a problem with %s'' % my_name


嗨斯图尔特,


另外一种方式,字符串 - > var而不是var - >字符串?


我的建议:

mylist = [" a"," b"," c"]

for my in mylist:

if locals()[my] == None:

print"你有%s的问题" %我的


Paolo


Stewart Midwinter写道:
Hi Stewart,

what about the other way, string -> var and not var -> string?

My suggestion:
mylist = ["a", "b", "c"]
for my in mylist:
if locals()[my] == None:
print "you have a problem with %s" % my

Paolo

Stewart Midwinter wrote:
我想做以下的事情:

a = 1; b = 2; c =无
mylist = [a,b,c]
我的mylist:
如果我是没有:
打印''你有问题%s''我的#this行是有问题的

I''d like to do something like the following:

a = 1; b = 2; c = None
mylist = [a, b, c]
for my in mylist:
if my is None:
print ''you have a problem with %s'' % my #this line is problematic

你有没有问题

我想看到什么在输出中是:
你有问题c
You have a problem with None

What I want to see in the output is:
You have a problem with c



如何将变量名称转换为字符串?

谢谢!


How do I convert a variable name into a string?

thanks!



这篇关于如何将变量名称转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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