
Quark's Outlines: Python Customizing Attribute Access
Quark’s Outlines: Python Customizing Attribute Access Overview, Historical Timeline, Problems & Solutions An Overview of Python Customizing Attribute Access What does it mean to customize attribute access in Python? When you use x.name in Python, the language looks for that name in the object’s data. This is called attribute access . You can customize attribute access in Python by defining special methods in a class. You can tell Python what to do when someone tries to get, set, or delete an attribute. These methods are __getattr__ , __setattr__ , and __delattr__ . They let you control how attributes behave at runtime. Python lets you change how attributes work using special methods. class Example : def __getattr__ ( self , name ): return f " Attribute { name } not found " x = Example () print ( x . foo ) # prints: # Attribute foo not found Python did not find foo , so it called __getattr__ . The method returned a message instead of raising an error. What does __getattr__ do in Python?
Continue reading on Dev.to Tutorial
Opens in a new tab




