Special Method Protocols

__getattr__() — attributes protocol

Resolve missing attributes after ordinary lookup raises AttributeError. Raise AttributeError for unsupported names so hasattr() and callers behave correctly.

Formal Syntax & Structure

python syntaxpython
def __getattr__(self, name):

Parameters & Attributes

NameTypeDescription
namestrMissing attribute name.

Return Value

object

Fallback attribute value.

Working Example

Example Snippetpython
class Config:    def __getattr__(self, name):        if name == "port": return 8000        raise AttributeError(name)print(Config().port)
Python Reference