Blog

Python getattr() Explained: When to Reach for It

Python's getattr() is one of those built-ins that's easy to skip until you need it. Then suddenly it's the cleanest tool for the job. Getattr() lets you fetch an attribute from an object by passing the attribute name as a string, and it handles the dynamic attribute lookup more elegantly than traditional methods.

At a glance

  • Getattr() lets you fetch an attribute from an object by passing the attribute name as a string.
  • It accepts three arguments: object, name, and an optional default.
  • It works for class attributes, instance attributes, function attributes, and methods.
  • __getattr__ is a special method called only when the attribute doesn't exist normally.

Why this matters

Most code never needs getattr(). But when attribute names come from user input, configuration files, or APIs, getattr() becomes the cleanest tool. The alternative—a long if/elif chain—is verbose and hard to maintain.

Trade-offs to consider: getattr() adds slight runtime overhead compared to direct attribute access. It's also less explicit, which can make debugging harder. Use it for dynamic lookups, but stick with dot notation for static attributes.

Winners: developers building flexible APIs, config systems, and dynamic object models. Losers: codebases where performance is critical and attribute names are known at compile time.

Who should care?

Use Case Relevance Why
CLI tools with dynamic flags High User input as attribute names
Plugin systems High Load attributes by string name
Data wrappers/mappers Medium Lazy attribute generation
General application code Low Direct access is faster

How the pieces fit together

An attribute is a characteristic of an object, written as object.attribute. Class attributes are shared across instances. Instance attributes live on the object and are defined in __init__. Function attributes are variables defined inside functions.

The getattr() function takes three arguments: object (the target), name (attribute name as a string), and an optional default returned when the attribute doesn't exist.

Getattr() works on methods too. Pass the method name as a string, assign the result to a variable, and call it. Same syntax as a regular attribute lookup.

__getattr__ is a hook that runs only when an attribute lookup fails. Getattr() is a function call. __getattr__ is a lifecycle method. __getattribute__ is different—it fires on every attribute access, not just missing ones.

A worked example

Say you're building a CLI tool that reads a config file and exposes settings as object attributes. Instead of writing a getter for each setting, you can use getattr() with a default:

“`python

Getattr(config, 'theme', 'dark')

“`

One line per setting. No boilerplate.

For the special-method side, implement __getattr__ to auto-generate missing fields. It runs only on missing attributes, so it won't shadow real ones—that's the behavior you want.

Bottom line

Getattr() is a small function with a specific use case: attribute lookup by name. __getattr__ is a hook for missing attributes. __getattribute__ runs on every access. Dir() rounds out the trio by listing available attributes. Learn the three together, and you'll handle dynamic attribute access cleanly.

Key takeaways

  • Getattr() is for attribute lookup by string, with an optional default.
  • __getattr__ runs only when an attribute lookup would otherwise fail.
  • __getattribute__ runs on every attribute access, not just missing ones.
  • Use getattr() to clean up long if/elif chains for attribute lookup.
  • Combine with dir() to inspect what's available on an object.

Frequently asked questions

What does getattr() do in Python?

It returns the value of an attribute from an object, given the attribute name as a string, with an optional default returned if the attribute doesn't exist.

How is getattr() different from __getattr__?

Getattr() is a built-in function you call directly. __getattr__ is a special method on a class that runs only when normal attribute lookup fails.

What about __getattribute__?

__getattribute__ is called on every attribute access, whether the attribute exists or not. __getattr__ only fires when an attribute is missing.

When should I use getattr()?

Use it when the attribute name is dynamic, comes from user input, or needs a default fallback without a long if/elif chain.

Can getattr() retrieve methods?

Yes. Pass the method name as a string, assign the result to a variable, and call it like a normal method.

Related