O'Reilly logo

Advanced Python 3 Programming Techniques by Mark Summerfield

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required