简体   繁体   English

从vars()和dict = {}创建的词典有什么区别?

[英]What is the difference between dictionaries created from vars() and dict={}?

After reading about vars(), I am still a little confused on what it is actually doing. 在阅读了关于vars()之后,我仍然对它实际做的事情感到有些困惑。 In looking at the Python documentation, "The vars() function returns the __ dict __ attribute of the given object if the object has __ dict __ attribute." 在查看Python文档时,“如果对象具有__ dict __属性,则vars()函数返回给定对象的__ dict __属性。” So, it returns a dictionary object? 那么,它返回一个字典对象? Here are the questions I am really getting at: 以下是我真正得到的问题:

1) What is vars() actually doing in the below code? 1)vars()在下面的代码中实际做了什么?

dict = {}
dict['H'] = atom
vars()[dict['H']] = 20

2) Why is the vars() necessary in front of the dictionary I created and could I leave it out? 2)为什么我创建的字典前面需要vars(),我可以把它留下来吗? (I know the code fails if I leave it out, but what would be a different way of accomplishing the same task?) (我知道如果我把它留下来代码就会失败,但是完成相同任务会有什么不同的方式?)

dict = {}
dict['H'] = atom
dict['H'] = 20

What is vars() actually doing in the below code? 什么是vars()实际上在下面的代码中做什么?

It does what it always does: it retrieves all local variables as a dictionary. 它做它总是做的事情:它将所有局部变量检索为字典。 That's how it works when calling vars without arguments. 这是在没有参数的情况下调用vars时的工作方式。

Why is the vars() necessary in front of the dictionary I created and could I leave it out? 为什么在我创建的字典前面需要vars(),我可以把它留下来吗?

It seems that someone is doing shenanigans with the local scope. 似乎有人在做当地范围的恶作剧。 It's just that it is wrapped with dict['H'] which is just some variable (possibly atom is a string). 只是它用dict['H']包裹,它只是一些变量(可能是atom是一个字符串)。 In other words he's trying to retrieve a variable by dynamic name. 换句话说,他试图通过动态名称检索变量。 For example 例如

>>> test = 15
>>> x = 'test'
>>> vars()[x]
15

This should be avoided at all costs. 应该不惜一切代价避免这种情况 Forget you even know about the existance of vars() . 忘记你甚至知道vars()的存在vars() I do not know a single case when you have to use vars() instead of a dict or some other "normal" (and in this context safe) structure. 当你不得不使用vars()而不是dict或其他“普通”(以及在此上下文安全)结构时,我不知道一个案例。 The code you are looking at is insanely difficult to maintain, error prone and has serious security issues. 您正在查看的代码非常难以维护,容易出错并且存在严重的安全问题。

1) What is vars() doing in the below code 1) vars()在下面的代码中做什么

Honestly, it's hard to tell. 老实说,这很难说。 vars() (without an argument) returns the local namespace as a dictionary. vars() (不带参数)将本地名称空间作为字典返回。 In other words, it does the same thing as locals() . 换句话说,它与locals()做同样的事情。 In your code, you then look up a name ( dict['H'] ) in the locals dict and set an item in there. 在你的代码中,然后在本地dict中查找一个名字( dict['H'] )并在那里设置一个项目。 Setting an item in the dict returned by locals() does nothing if you are in a function... If you are in the global namespace, it adds an attribute. 如果你在函数中,设置locals()返回的dict中的项什么也不做......如果你在全局命名空间中,它会添加一个属性。

2) Why is the vars() necessary in front of the dictionary I created and could I leave it out? 2)为什么我创建的字典前面需要vars() ,我可以把它留下来吗?

It probably isn't necessary and you probably should leave it out. 它可能没有必要,你可能应该把它留下来。 Normally dynamically setting attributes on the global namespace is a really bad idea. 通常在全局命名空间上动态设置属性是一个非常糟糕的主意。 You should generally pass around a dictionary of the data that you need instead. 您通常应该传递一个您需要的数据字典。 In other words, you can do: 换句话说,你可以这样做:

v = 'foo'
globals()[v] = 'bar'
# ...
value = globals()[v]  # 'bar'

But you shouldn't . 但你不应该 It's much better to just pass a dictionary around: 只需传递一本字典就好了:

v = 'foo'
dct = {v: 'bar'}
# ...
value = dct[v]

First, you shouldn't overwrite __builtin__ functions. 首先,您不应该覆盖__builtin__函数。 dict is a reserved name for a function used to create dictionary objects. dict是用于创建字典对象的函数的保留名称。

Second, var gives you access to name declarations within the current running scope. 其次, var允许您访问当前运行范围内的名称声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM