Skip to main content
Editor

__getattr__ and __setattr__: Dynamic Attribute Access in Python

Advanced25 min5 exercises100 XP
0/5 exercises

Most of the time, Python attributes are straightforward. You set obj.x = 5, and later obj.x gives you 5. But what if you want attributes that don't actually exist - attributes that are computed on the fly, or that trigger side effects when accessed?

Python gives you full control over attribute access through special methods: __getattr__, __getattribute__, __setattr__, and __delattr__. These let you intercept every read, write, and delete operation on any attribute.

Think of these methods as a security desk at a building entrance. Every time someone tries to enter (read), drop something off (write), or remove something (delete), the desk can check, log, redirect, or deny the request.

What Is __getattr__ and When Does It Run?

__getattr__ is Python's fallback for attribute access. It's only called when the normal lookup fails - when the attribute doesn't exist in the instance dict, the class, or any parent class.

__getattr__ is a fallback
class FlexibleConfig:
def __init__(self):
self.name = 'app'
self.version = '1.0'
def __getattr__(self, name):
# Called only when normal lookup fails
print(f' __getattr__ called for: {name}')
return f'default_{name}'
config = FlexibleConfig()
# These exist - __getattr__ is NOT called
print(config.name) # app
print(config.version) # 1.0
# These don't exist - __getattr__ IS called
print(config.database) # default_database
print(config.timeout) # default_timeout
Output
app
1.0
  __getattr__ called for: database
default_database
  __getattr__ called for: timeout
default_timeout

This is the key thing to remember: __getattr__ is a fallback. It only triggers for missing attributes. Attributes that exist in __dict__ or on the class are found through normal lookup and never hit __getattr__.

What Is the Difference Between __getattr__ and __getattribute__?

This is one of the most confusing distinctions in Python:

  • __getattr__ - called only when the attribute is not found through normal means
  • __getattribute__ - called on every single attribute access, even for attributes that exist
  • __getattribute__ is the nuclear option. It intercepts everything.

    __getattribute__ intercepts everything
    class Logged:
    def __init__(self):
    # Must use super().__setattr__ to avoid recursion
    super().__setattr__('data', {'x': 10})
    def __getattribute__(self, name):
    print(f' Accessing: {name}')
    return super().__getattribute__(name)
    obj = Logged()
    print(obj.data) # Intercepted!
    print(obj.data) # Intercepted again!
    Output
      Accessing: data
    {'x': 10}
      Accessing: data
    {'x': 10}

    Here's how the full lookup chain works:

  • __getattribute__ is called (always)
  • It checks data descriptors on the class
  • It checks instance.__dict__
  • It checks non-data descriptors and class attributes
  • If nothing is found, __getattr__ is called as a last resort
  • How Do You Intercept Setting and Deleting Attributes?

    __setattr__ is called every time an attribute is set - including inside __init__. This means you need to be careful to avoid infinite recursion:

    Auditing all attribute changes
    class Audited:
    def __init__(self, name):
    self.name = name # This triggers __setattr__!
    def __setattr__(self, name, value):
    print(f' Setting {name} = {value!r}')
    # Must use super() or object.__setattr__ to actually set it
    super().__setattr__(name, value)
    def __delattr__(self, name):
    print(f' Deleting {name}')
    super().__delattr__(name)
    obj = Audited('Alice')
    obj.age = 30
    del obj.age
    print(obj.name)
    Output
      Setting name = 'Alice'
      Setting age = 30
      Deleting age
    Alice

    The pattern is the same as __getattribute__: inside __setattr__, you can't write self.x = value normally (that would call __setattr__ again). Use super().__setattr__(name, value) to actually store the attribute.

    Wrong: Causes infinite recursion
    class Bad:
        def __setattr__(self, name, value):
            self.log = []  # Recursion!
            self.__dict__[name] = value
    Right: Use super() or __dict__
    class Good:
        def __setattr__(self, name, value):
            # Option 1: super()
            super().__setattr__(name, value)
            # Option 2: direct __dict__
            # self.__dict__[name] = value

    What Are Real-World Uses for Dynamic Attributes?

    Dynamic attribute access enables powerful patterns that would be verbose or impossible otherwise. Here are some of the most useful ones.

    Dot-notation dictionary access
    class DotDict:
    '''Access dictionary keys as attributes.'''
    def __init__(self, data):
    super().__setattr__('_data', data)
    def __getattr__(self, name):
    try:
    return self._data[name]
    except KeyError:
    raise AttributeError(f'No attribute: {name}')
    def __setattr__(self, name, value):
    self._data[name] = value
    config = DotDict({'host': 'localhost', 'port': 5432})
    print(config.host) # localhost
    print(config.port) # 5432
    config.debug = True
    print(config.debug) # True
    print(config._data) # Full dict
    Output
    localhost
    5432
    True
    {'host': 'localhost', 'port': 5432, 'debug': True}

    Notice the use of super().__setattr__('_data', data) in __init__. We can't use self._data = data because that would trigger our custom __setattr__, which tries to access self._data - but it doesn't exist yet!

    Deprecation warnings for old attribute names
    import warnings
    class DeprecatedAttrs:
    '''Warn when deprecated attribute names are used.'''
    _deprecated = {
    'colour': 'color',
    'favourite': 'favorite',
    }
    def __init__(self):
    self.color = 'red'
    self.favorite = 'pizza'
    def __getattr__(self, name):
    if name in self._deprecated:
    new_name = self._deprecated[name]
    print(f'Warning: \'{name}\' is deprecated, use \'{new_name}\'')
    return getattr(self, new_name)
    raise AttributeError(f'No attribute: {name}')
    obj = DeprecatedAttrs()
    print(obj.color) # Normal - no warning
    print(obj.colour) # Deprecated - shows warning
    Output
    red
    Warning: 'colour' is deprecated, use 'color'
    red

    What Is Python's Complete Attribute Lookup Chain?

    Here's the complete picture of what happens when you access obj.x:

  • type(obj).__getattribute__(obj, 'x') is called
  • Check for a data descriptor named x on type(obj) and its MRO
  • Check obj.__dict__['x'] (the instance dictionary)
  • Check for a non-data descriptor or class variable named x on type(obj) and its MRO
  • If nothing found, call obj.__getattr__('x') (if defined)
  • Raise AttributeError
  • And for setting obj.x = value:

  • type(obj).__setattr__(obj, 'x', value) is called
  • Check for a data descriptor named x on type(obj) - if found, call its __set__
  • Otherwise, store value in obj.__dict__['x']

  • Practice Exercises

    Build a Default Dictionary Object
    Write Code

    Create a class DefaultObj that returns a default value for any attribute that doesn't exist. The constructor takes the default value.

    Regular attributes should work normally. Only missing attributes should return the default.

    obj = DefaultObj('N/A')
    obj.name = 'Alice'
    print(obj.name)      # Alice
    print(obj.missing)   # N/A
    print(obj.whatever)  # N/A
    class DefaultObj:
    # Your code here
    pass
    obj = DefaultObj('N/A')
    obj.name = 'Alice'
    print(obj.name)
    print(obj.missing)
    print(obj.whatever)
    Predict the Attribute Access
    Predict Output

    What does the following code print?

    class Magic:
        x = 10
        
        def __getattr__(self, name):
            return f'fallback:{name}'
    
    obj = Magic()
    obj.y = 20
    print(obj.x)
    print(obj.y)
    print(obj.z)

    Remember: __getattr__ is only called when normal lookup fails.

    # Type your predicted output
    Build a Read-Only Object
    Write Code

    Create a class Frozen that allows attributes to be set in __init__ but raises AttributeError with the message 'Cannot modify frozen object' if anyone tries to set an attribute after initialization.

    Use a flag _frozen to track whether initialization is complete.

    obj = Frozen(name='Alice', age=30)
    print(obj.name)  # Alice
    print(obj.age)   # 30
    class Frozen:
    def __init__(self, **kwargs):
    for key, value in kwargs.items():
    # Need to set attributes without triggering the freeze
    pass
    # Mark as frozen
    pass
    # Test it
    obj = Frozen(name='Alice', age=30)
    print(obj.name)
    print(obj.age)
    Fix the Recursive __setattr__
    Fix the Bug

    This code tries to log attribute changes, but it crashes with infinite recursion. Fix it so it prints:

    Setting name = Alice
    Setting age = 30
    Alice is 30
    class LoggedAttrs:
    def __init__(self, name, age):
    self.name = name
    self.age = age
    def __setattr__(self, name, value):
    print(f'Setting {name} = {value}')
    # Bug: this calls __setattr__ again!
    self.name = value
    obj = LoggedAttrs('Alice', 30)
    print(f'{obj.name} is {obj.age}')
    Build a Proxy Object
    Write Code

    Create a Proxy class that wraps another object and forwards all attribute access to it. The proxy should also count how many times attributes were accessed.

    The constructor takes the wrapped object. Add a method get_access_count() that returns the total number of attribute accesses on the wrapped object.

    class Person:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
    p = Person('Alice', 30)
    proxy = Proxy(p)
    print(proxy.name)          # Alice
    print(proxy.age)           # 30
    print(proxy.name)          # Alice
    print(proxy.get_access_count())  # 3
    class Person:
    def __init__(self, name, age):
    self.name = name
    self.age = age
    class Proxy:
    # Your code here
    pass
    p = Person('Alice', 30)
    proxy = Proxy(p)
    print(proxy.name)
    print(proxy.age)
    print(proxy.name)
    print(proxy.get_access_count())

    Harder problems

    More problems on this topic: Classes

    Related Tutorials

    Was this tutorial helpful?