vars()
IntrospectionReturns the __dict__ attribute of an object (its writable attributes).
Signature
vars(object)
Returns
dictExample
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
p = Point(3, 4)
print(vars(p)) # {'x': 3, 'y': 4}About vars()
vars is a Python introspection function with the signature vars(object). Returns the __dict__ attribute of an object (its writable attributes). It returns a value of type dict.
Python provides a rich set of built-in functions and standard library modules that cover common programming tasks. Understanding these functions helps you write more idiomatic, efficient Python code. The varsfunction is commonly used in data processing, web development, scripting, and automation tasks.
When working with vars(), consider edge cases like empty inputs, None values, and type mismatches. Python's duck typing means many built-in functions work with any object that implements the required protocol (e.g., __len__ for len(), __iter__ for iteration). This flexibility is a key strength of Python's design philosophy.