你怎么得到字典的名字? [英] how do you get the name of a dictionary?

查看:69
本文介绍了你怎么得到字典的名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


有谁知道如何找到python数据类型的名称。


考虑字典:


Banana = {}


然后,我如何向python询问一个表示

上面字典名称的字符串(即''Banana'')?


感谢任何有时间回答这个nube问题的人!

荷荷巴

解决方案



jojoba写道:


您好!


有谁知道如何找到python数据类型的名称。


考虑字典:


Banana = {}


然后,我如何向python询问一个表示字典上面的

名称的字符串(即''Banana'')?


感谢任何有时间回答这个nube问题的人!

荷荷巴



这里是一个容易的黑客,我不知道是否re是一个明确的函数。

for i in dir():

if eval(i)== Banana:

print i


有谁知道如何找到python数据类型的名称。


>

考虑字典:


香蕉= {}


然后,我如何向python询问一个字符串,表示字典上面的

的名称(即''Banana'')?



AFAIK,没有简单/好的方法可以做到这一点因为这个名字

只是内部对象的句柄。观察:


>> banana = {}
spatula = banana < spatula [42] =''drangle''
banana



{42:''drangle'' }


>> id(spatula)



10304800


> > id(香蕉)



10304800


请求什么意思对象ID 10304800的名称?

它们都是香蕉和刮刀。一个人可能可以使用

反编译库并在一个人的方式中穿过黑暗的凹陷来获取这些信息,但是这是

肯定不是初学者的任务。


你会如何要求对象?

< blockquote class =post_quotes>


>> print get_name(banana)



你不妨写一下


>> print" banana"



:)


希望这有意义......


-tkc


Andy Terrel写道:


for i in dir() :

如果eval(i)==香蕉:

打印i



(头部击打桌子的声音) )


< / F>


Hello!

Does anyone know how to find the name of a python data type.

Conside a dictionary:

Banana = {}

Then, how do i ask python for a string representing the name of the
above dictionary (i.e. ''Banana'')?

thanks to anyone who has time to answer this nube question!
jojoba

解决方案


jojoba wrote:

Hello!

Does anyone know how to find the name of a python data type.

Conside a dictionary:

Banana = {}

Then, how do i ask python for a string representing the name of the
above dictionary (i.e. ''Banana'')?

thanks to anyone who has time to answer this nube question!
jojoba

here is an easy hack, I don''t know if there is an explicit function.
for i in dir():
if eval(i) == Banana:
print i


Does anyone know how to find the name of a python data type.

>
Conside a dictionary:

Banana = {}

Then, how do i ask python for a string representing the name of the
above dictionary (i.e. ''Banana'')?

AFAIK, there''s no easy/good way of doing this because that name
is just a handle to an internal object. Observe:

>>banana = {}
spatula = banana
spatula[42] = ''drangle''
banana

{42: ''drangle''}

>>id(spatula)

10304800

>>id(banana)

10304800

What does it mean to ask for the name of object ID 10304800?
it''s both "banana" and "spatula". One might be able to use the
decompiler libraries and wend one''s way through the dark recesses
of python''s internals to extract such information, but this is
certainly not a beginner''s task.

How would you ask for the object?

>>print get_name(banana)

you might as well write

>>print "banana"

:)

Hope this makes sense...

-tkc


Andy Terrel wrote:

for i in dir():
if eval(i) == Banana:
print i

(sound of head hitting desk)

</F>


这篇关于你怎么得到字典的名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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