📜  Python中的 vars()函数

📅  最后修改于: 2022-05-13 01:54:29.525000             🧑  作者: Mango

Python中的 vars()函数

这是Python中的内置函数。 vars() 方法只接受一个参数,这也是可选的。它将对象作为参数,可以是模块、类、实例或任何具有 __dict__ 属性的对象。
句法:

vars(object)

如果模块、类、实例或任何其他对象具有 __dict__ 属性,则该方法返回 __dict__ 属性。如果对象无法匹配属性,则会引发 TypeError 异常。模块和实例等对象具有可更新的 __dict__ 属性,但是,其他对象可能对其 __dict__ 属性有书面限制。当传递一个空参数时,vars() 的作用类似于 locals() 方法,这意味着 locals 字典仅对读取有用,因为对 locals 字典的更新被忽略了。

Python3
# Python program to illustrate
# working of vars() method in Python
 
class Geeks:
  def __init__(self, name1 = "Arun", num2 = 46, name3 = "Rishab"):
    self.name1 = name1
    self.num2 = num2
    self.name3 = name3
   
GeeksforGeeks = Geeks()
print(vars(GeeksforGeeks))


Python3
# Python program to illustrating
# the use of vars() and locals
# when no argument is passed and
# how vars() act as locals().
class Geeks(object):
    def __init__(self):
        self.num1 = 20
        self.num2 = "this is returned"
 
    def __repr__(self):
        return "Geeks() is returned"
 
    def loc(self):
        ans = 21
        return locals()
     
    # Works same as locals()
    def code(self):
        ans = 10
        return vars()
 
    def prog(self):
        ans = "this is not printed"
        return vars(self)
     
 
if __name__ == "__main__":
    obj = Geeks()
    print (obj.loc())
    print (obj.code())
    print (obj.prog())


输出:

{'num2': 46, 'name1': 'Arun', 'name3': 'Rishab'}

Python3

# Python program to illustrating
# the use of vars() and locals
# when no argument is passed and
# how vars() act as locals().
class Geeks(object):
    def __init__(self):
        self.num1 = 20
        self.num2 = "this is returned"
 
    def __repr__(self):
        return "Geeks() is returned"
 
    def loc(self):
        ans = 21
        return locals()
     
    # Works same as locals()
    def code(self):
        ans = 10
        return vars()
 
    def prog(self):
        ans = "this is not printed"
        return vars(self)
     
 
if __name__ == "__main__":
    obj = Geeks()
    print (obj.loc())
    print (obj.code())
    print (obj.prog())

输出:

{'ans': 21, 'self': Geeks() is returned}
{'ans': 10, 'self': Geeks() is returned}
{'num1': 20, 'num2': 'this is returned'}