Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

getattr

getattr() retrieves an attribute using a string name. It can return a fallback for missing attributes or raise AttributeError when no fallback is supplied.

How it works

getattr() receives an object and a string naming an attribute, then returns that attribute's value. An optional default is returned when the attribute is missing; without one, the lookup raises AttributeError. Attribute names do not have to be valid Python identifiers.

Syntax

getattr(object, name[, default])

Parameters

NameTypeDescription
objectobjectThe object whose attribute is inspected.
namestrA string containing the attribute name to retrieve.
defaultobject, optionalFallback returned if the named attribute does not exist.

Returns

The value of the named attribute, or default when supplied and the attribute is absent; otherwise it raises AttributeError.

Key points

  • It reads and returns the value associated with the requested attribute.
  • The attribute name argument must be a string.
  • An existing named attribute produces its current value.
  • For a literal attribute name, dynamic lookup has the same result as ordinary dot access.
  • Names containing characters disallowed in Python identifiers can still be requested.
  • setattr() is the related write operation for attribute access.

Common mistakes

  • Omitting the default makes a missing attribute an AttributeError rather than a fallback value.
  • Double-underscore private names require manual name mangling when retrieved dynamically.

Example: Dynamic attribute lookup

class Config:
    timeout = 30

config = Config()
name = "timeout"
print(getattr(config, name, 10))
print(getattr(config, "missing", 10))

Looks up an attribute whose name is stored in a string and supplies 10 when that attribute is absent.

When to use it

Dynamic field access

A program receives a field name from configuration or another runtime source and must inspect that field without hard-coding dot notation.

Fallback for optional attributes

Code supports optional attributes across objects with different interfaces and needs a value when a field is unavailable.

Generic attribute utilities

A framework or utility needs to pair runtime attribute reads with the documented attribute-writing counterpart, setattr().

Interview questions

QWhat does getattr() do when the attribute is missing?

getattr() takes an object and a string attribute name, returning the named attribute's value. Its optional third argument is returned if the attribute is absent; otherwise the function raises AttributeError.

Follow-up: Can the name contain characters that are not valid in an identifier? Yes: the documentation says the name need not be a Python identifier.

QWhat are getattr()'s argument requirements?

The attribute name must be a string, and the function performs a dynamic lookup equivalent to accessing that attribute directly when it exists. The optional default distinguishes a fallback lookup from an exception-producing lookup.

Follow-up: What happens with a double-underscore attribute? The caller must manually apply the name-mangling form because mangling occurs during compilation.

QHow does getattr() relate to setattr()?

setattr() is described as getattr()'s counterpart. getattr() reads a named attribute, whereas setattr() assigns an arbitrary value to an existing or new attribute when the object permits it.

Follow-up: When might getattr() be preferable to direct access? When the attribute name is computed or when a documented fallback is needed for absent attributes.

  • setattr()

Source: Built-in functions. Checked 2026-10-16.