Chapter 8. Controlling Attribute Access

Let’s start with one very small and simple new feature. Here is the start of the definition of a simple Point class:

class Point:    __slots__ = ("x", "y")    def __init__(self, x=0, y=0):        self.x = x        self.y = y

When a class is created without the use of __slots__, behind the scenes Python creates a private dictionary called __dict__ for each instance, and this dictionary holds the instance’s data attributes. This is why we can add or remove attributes from objects. (For example, we added a cache attribute to the get_function() function earlier in this short cut.)

If we only need objects where we access the original attributes and don’t need to add ...

Get Advanced Python 3 Programming Techniques now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.