小问:如何打印变量的名称,而不是它的值? [英] Little Q: how to print a variable's name, not its value?

查看:84
本文介绍了小问:如何打印变量的名称,而不是它的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

毫无疑问,我忽略了一些显而易见的事情,但这里有:


让我们说我为var赋值,例如:

myPlace =''就在这里''

myTime =''现在''


现在让我们说要打印出来两个变量,以及它们的名字。

我可以很容易地做到这一点:

print" myPlace =%s,myTime =%s" %(myPlace,myTime)


但这要求我提前知道变量的名称。如果

它们是动态分配的。我正在寻找的是一些方法(

称之为f()),允许我这样做:

print"%s =%s,% s =%s %(f(myPlace),myPlace,f(myTime),myTime)


任何想法?


repr()不会为我做,也不是``。


谢谢

斯图尔特在卡尔加里

No doubt I''ve overlooked something obvious, but here goes:

Let''s say I assign a value to a var, e.g.:
myPlace = ''right here''
myTime = ''right now''

Now let''s say I want to print out the two vars, along with their names.
I could easily do this:
print "myPlace = %s, myTime = %s" % (myPlace, myTime)

But that requires that I know in advance the name of the vars. What if
they are assigned dynamically. What I''m looking for is some method (
call it f() ) that allows me to do this:
print "%s = %s, %s = %s" % (f(myPlace), myPlace, f(myTime), myTime)

Any ideas?

repr() doesn''t do it for me, nor ``.

thanks
Stewart in Calgary

推荐答案

st ************ ***@gmail.com 2005-03-29 01:06对世界说:
st***************@gmail.com said unto the world upon 2005-03-29 01:06:
毫无疑问,我忽略了一些显而易见的事情,但这里有:

让我说我给一个var赋值,例如:
myPlace =''就在这里''
我的时间=''现在''
我可以很容易地做到这一点:
print" myPlace =%s,myTime =%s" %(myPlace,myTime)

但这要求我事先知道变量的名称。如果它们是动态分配的,那该怎么办?我正在寻找的是一些允许我这样做的方法(
称之为f()):
print"%s =%s,%s =%s" %(f(myPlace),myPlace,f(myTime),myTime)

任何想法?

repr()不是为我做的,也不是`` 。

谢谢
斯图尔特在卡尔加里
No doubt I''ve overlooked something obvious, but here goes:

Let''s say I assign a value to a var, e.g.:
myPlace = ''right here''
myTime = ''right now''

Now let''s say I want to print out the two vars, along with their names.
I could easily do this:
print "myPlace = %s, myTime = %s" % (myPlace, myTime)

But that requires that I know in advance the name of the vars. What if
they are assigned dynamically. What I''m looking for is some method (
call it f() ) that allows me to do this:
print "%s = %s, %s = %s" % (f(myPlace), myPlace, f(myTime), myTime)

Any ideas?

repr() doesn''t do it for me, nor ``.

thanks
Stewart in Calgary




使用字典而不是自由浮动变量来存储数据。


Stewarts_data = {''myPlace'':''就在这里'',

''myTime'':'''现在''}

for Stewarts_data.items()中的项目:

打印''%s =%s''%项目


(有可能更好方法 - 例如在Python 2.4中你可以在字典中使用sorted() - 但这可以解决这个想法,我希望.b $ b希望。)


Best,

Brian vdB



Use a dictionary instead of free floating variables to store your data.

Stewarts_data = { ''myPlace'' : ''right here'',
''myTime'' : ''right now'' }
for item in Stewarts_data.items():
print ''%s = %s'' %item

(There are likely even nicer ways -- for instance in Python 2.4 you
can use sorted() on the dictionary -- but this gets the idea across, I
hope.)

Best,

Brian vdB


st *************** @ gmail.com 写道:
毫无疑问,我忽略了一些显而易见的事情,但这里有:

让我们说我赋予一个值一个var,例如:
myPlace =''就在这里''
myTime =''现在''

现在让我们说我要打印两个vars,以及
名称。我可以轻松地做到这一点:
print" myPlace =%s,myTime =%s" %(myPlace,myTime)

但这要求我事先知道变量的名称。如果动态分配,
是多少?我正在寻找的是一些允许我这样做的方法(
称之为f()):
print"%s =%s,%s =%s" %(f(myPlace),myPlace,f(myTime),myTime)

任何想法?
No doubt I''ve overlooked something obvious, but here goes:

Let''s say I assign a value to a var, e.g.:
myPlace = ''right here''
myTime = ''right now''

Now let''s say I want to print out the two vars, along with their names. I could easily do this:
print "myPlace = %s, myTime = %s" % (myPlace, myTime)

But that requires that I know in advance the name of the vars. What if they are assigned dynamically. What I''m looking for is some method (
call it f() ) that allows me to do this:
print "%s = %s, %s = %s" % (f(myPlace), myPlace, f(myTime), myTime)

Any ideas?




尝试这样的事情:



Try something like this:

def print_vars(vars_dict = None):
....如果vars_dict为None:

.... vars_dict = globals()

.... for var,vars_dict.items()中的值:

.... print ''%s =%r''%(var,value)

.... myPlace =''就在这里''
myTime =''现在'' print_vars()
def print_vars(vars_dict=None): .... if vars_dict is None:
.... vars_dict = globals()
.... for var, value in vars_dict.items():
.... print ''%s = %r'' % (var, value)
.... myPlace = ''right here''
myTime = ''right now''
print_vars()



print_vars =< function print_vars at 0x401e0d84>

__builtins__ =< module''__ builtin__' '(内置)>

myTime =''现在''

myPlace =''这里''

__name__ = ''__main__''

__doc__ =无


print_vars = <function print_vars at 0x401e0d84>
__builtins__ = <module ''__builtin__'' (built-in)>
myTime = ''right now''
myPlace = ''right here''
__name__ = ''__main__''
__doc__ = None


2005年3月28日22:06:44 -0800,谣言说

&qu OT; ST *************** @ gmail.com" < ST *************** @ gmail.com>可能有

写的:


< snip complete article--主题就够了


了解当地人Python文档中的()和globals()。这些

提供您要求的信息(即什么名称绑定到什么

对象)。

-

TZOTZIOY,我说英格兰最好。

发送时要严格,接收时要宽容。 (来自RFC1958)

我真的应该在与人交谈时牢记这一点,实际上......
On 28 Mar 2005 22:06:44 -0800, rumours say that
"st***************@gmail.com" <st***************@gmail.com> might have
written:

<snip complete article-- subject is enough>

Read about locals() and globals() in the Python documentation. These
provide the information you request (ie what names are bound to what
objects).
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...


这篇关于小问:如何打印变量的名称,而不是它的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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