vars()和__dict__是相同的吗?

huangapple go评论64阅读模式
英文:

Is vars() same as __dict__?

问题

vars() 是一个内置函数,返回类、模块或对象的 __dict__ 属性。但是,当我检查 vars(Person) is Person.__dict__ 时,它返回 FalsePerson 是类的名称)。

英文:

I read that vars() is a built-in that returns the __dict__ attribute of class, module or object. But when I checked for vars(Person) is Person.__dict__, it returned False(Person is the name of class).

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

vars(Person) is Person.__dict__  # False

</details>


# 答案1
**得分**: 4

Classes create a new mappingproxy on every `__dict__` access. You would see the exact same results from `Person.__dict__ is Person.__dict__`.

<details>
<summary>英文:</summary>

Classes create a new mappingproxy on every `__dict__` access. You would see the exact same results from `Person.__dict__ is Person.__dict__`.

</details>



huangapple
  • 本文由 发表于 2023年6月8日 02:26:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定