{"id":24883,"date":"2021-11-10T09:36:01","date_gmt":"2021-11-10T04:06:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=24883"},"modified":"2021-11-10T09:36:01","modified_gmt":"2021-11-10T04:06:01","slug":"python-vars-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-vars-function-with-examples\/","title":{"rendered":"Python vars() Function with Examples"},"content":{"rendered":"
vars() Function in Python:<\/strong><\/p>\n The vars() function returns an object’s __dict__ attribute.<\/p>\n The __dict__ attribute is a dictionary that contains a list of the object’s changeable attributes.<\/p>\n Note:<\/strong><\/p>\n It should be noted that calling the vars() function without any parameters will result in the return of a dictionary containing the local symbol table.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n object:<\/strong> It may be Any object that has the __dict__ attribute.<\/p>\n Return Value:<\/strong><\/p>\n vars() returns the given object’s __dict__ attribute.<\/p>\n Note:<\/strong> It should be noted that __dict__ refers to a dictionary or a mapping object. It stores the (writable) attributes of an object.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" vars() Function in Python: The vars() function returns an object’s __dict__ attribute. The __dict__ attribute is a dictionary that contains a list of the object’s changeable attributes. Note: It should be noted that calling the vars() function without any parameters will result in the return of a dictionary containing the local symbol table. Syntax: vars(object) …<\/p>\nvars(object)<\/pre>\n
\n
vars() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Create a class say, Employee.\r\nclass Employee:\r\n # Take a variable and initialize it with some random number(eid).\r\n eid = 5\r\n # Take another variable and initialize it with some random name(ename).\r\n ename = \"kevin\"\r\n # Take another variable and initialize it with some random job role(jobrole).\r\n jobrole = \"software developer\"\r\n\r\n\r\n# Create an object for the above class and apply the vars() method to it\r\n# Store it in a variable.\r\nrslt = vars(Employee)\r\n# Print the above result.\r\nprint(rslt)\r\n<\/pre>\n
{'__module__': '__main__', 'eid': 5, 'ename': 'kevin', 'jobrole': 'software developer', '__dict__': <attribute '__dict__' of 'Employee' objects>, '__weakref__': <attribute '__weakref__' of 'Employee' objects>, '__doc__': None}<\/pre>\n