What is the hasattr function in Python?
hasattr() returns True if an object has the named attribute. See how it works, its pitfalls with properties, and the better alternatives.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
hasattr() returns True if an object has the named attribute. See how it works, its pitfalls with properties, and the better alternatives.
vars() returns an object's __dict__ or the local namespace when called without arguments. See syntax, examples, limits and how it differs from dir().
locals() returns a mapping of local names to values. Learn why edits inside functions are ignored and what changed in Python 3.13 with PEP 667.
globals() returns the live dictionary of a module's global namespace. See how to read it, change it, and when not to use it versus locals().
dir() returns a sorted list of the names in the current scope or the attributes of an object. See how it works, how to customise it, and how it differs from vars().
Python's type() returns an object's class and can also build classes at runtime. Learn when to use it, and why isinstance() is usually better.
issubclass() checks whether one class derives from another. Learn tuples, ABCs, virtual subclasses and how it differs from isinstance().
isinstance() checks whether an object is an instance of a class or subclass. Learn tuples, unions, the bool trap and how it differs from type().
Python's super() calls the next class in the method resolution order. See how it works for single and multiple inheritance, with runnable examples.
The __call__ method lets you call an object like a function. See how it works, how it keeps state, and where it fits in decorators and callbacks.
The Python __repr__ method returns a developer-focused string for an object. See how it differs from __str__ and how to write a good one.
The __str__ method defines the readable text form of a Python object, used by print(), str() and f-strings. See how it differs from __repr__ with examples.
__init__ initializes a new Python object right after it is created. Learn defaults, super(), the mutable default trap and dataclasses.
A list comprehension builds everything in memory now, a generator produces values lazily. Compare memory, speed and reuse and pick the right one.
yield pauses a Python function, returns a value and keeps its state, creating a generator. Learn how it differs from return and when to use it.
A closure in Python is a nested function that keeps variables from its enclosing scope alive. See nonlocal, decorators and common pitfalls.
A Python decorator wraps a function to add behaviour without changing its code. See how @ works, how to pass arguments, and how to keep type hints in Python 3.12.
Deep copy a Python dictionary with copy.deepcopy(). See how it differs from dict.copy(), how it handles nested data, and when other approaches fit.
Deep copy a Python list with copy.deepcopy(). Understand shallow vs deep copies, the nested list multiplication bug, and faster alternatives.
Learn every way to copy a Python dictionary, when a shallow copy is enough, and when copy.deepcopy() is needed for nested data.