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