vars() — Python Technical Interview Questions & Answers
vars()
vars()
- vars() is built into Python. You do not import it. Freshers only need one honest call and the printed result — not a long definition.
- vars() — output: {'name': 'Asha'}. vars shows instance __dict__.
- Wrong type often raises TypeError. Empty values matter for bool/all/any. Don’t mix vars up with a similar name (sorted vs sort, int vs float).
Technical Interview Tip: Call vars() once and say what prints.
Code Examples for vars()
Example
# vars()
class S:
def __init__(self):
self.name = "Asha"
print(vars(S()))
vars() — output: {'name': 'Asha'}. vars shows instance __dict__.
How to Explain vars() to an Interviewer
Explain vars() as if you are teaching a junior — definition, then one tiny script.
vars() is built-in — no import. One call + exact output.
What does the example print, and why?
vars() — output: {'name': 'Asha'}
What mistake do freshers make with vars()?
Wrong type → TypeError.