hasattr() — Python Technical Interview Questions & Answers

hasattr()

hasattr()

  • hasattr() is built into Python. You do not import it. Freshers only need one honest call and the printed result — not a long definition.
  • hasattr() — output: True. str has upper.
  • Wrong type often raises TypeError. Empty values matter for bool/all/any. Don’t mix hasattr up with a similar name (sorted vs sort, int vs float).

Technical Interview Tip: Call hasattr() once and say what prints.

Code Examples for hasattr()

Example

# hasattr()
print(hasattr("Asha", "upper"))

hasattr() — output: True. str has upper.

How to Explain hasattr() to an Interviewer

Explain hasattr() as if you are teaching a junior — definition, then one tiny script.

hasattr() is built-in — no import. One call + exact output.

What does the example print, and why?

hasattr() — output: True

What mistake do freshers make with hasattr()?

Wrong type → TypeError.