Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1 | .. _descriptorhowto: |
| 2 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 3 | ====================== |
| 4 | Descriptor HowTo Guide |
| 5 | ====================== |
| 6 | |
| 7 | :Author: Raymond Hettinger |
| 8 | :Contact: <python at rcn dot com> |
| 9 | |
| 10 | .. Contents:: |
| 11 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 12 | |
| 13 | :term:`Descriptors <descriptor>` let objects customize attribute lookup, |
| 14 | storage, and deletion. |
| 15 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 16 | This guide has four major sections: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 17 | |
| 18 | 1) The "primer" gives a basic overview, moving gently from simple examples, |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 19 | adding one feature at a time. Start here if you're new to descriptors. |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 20 | |
| 21 | 2) The second section shows a complete, practical descriptor example. If you |
| 22 | already know the basics, start there. |
| 23 | |
| 24 | 3) The third section provides a more technical tutorial that goes into the |
| 25 | detailed mechanics of how descriptors work. Most people don't need this |
| 26 | level of detail. |
| 27 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 28 | 4) The last section has pure Python equivalents for built-in descriptors that |
| 29 | are written in C. Read this if you're curious about how functions turn |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 30 | into bound methods or about the implementation of common tools like |
| 31 | :func:`classmethod`, :func:`staticmethod`, :func:`property`, and |
| 32 | :term:`__slots__`. |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 33 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 34 | |
| 35 | Primer |
| 36 | ^^^^^^ |
| 37 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 38 | In this primer, we start with the most basic possible example and then we'll |
| 39 | add new capabilities one by one. |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 40 | |
| 41 | |
| 42 | Simple example: A descriptor that returns a constant |
| 43 | ---------------------------------------------------- |
| 44 | |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 45 | The :class:`Ten` class is a descriptor that always returns the constant ``10`` |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 46 | from its :meth:`__get__` method: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 47 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 48 | .. testcode:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 49 | |
| 50 | class Ten: |
| 51 | def __get__(self, obj, objtype=None): |
| 52 | return 10 |
| 53 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 54 | To use the descriptor, it must be stored as a class variable in another class: |
| 55 | |
| 56 | .. testcode:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 57 | |
| 58 | class A: |
| 59 | x = 5 # Regular class attribute |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 60 | y = Ten() # Descriptor instance |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 61 | |
| 62 | An interactive session shows the difference between normal attribute lookup |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 63 | and descriptor lookup: |
| 64 | |
| 65 | .. doctest:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 66 | |
| 67 | >>> a = A() # Make an instance of class A |
| 68 | >>> a.x # Normal attribute lookup |
| 69 | 5 |
| 70 | >>> a.y # Descriptor lookup |
| 71 | 10 |
| 72 | |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 73 | In the ``a.x`` attribute lookup, the dot operator finds the key ``x`` and the |
| 74 | value ``5`` in the class dictionary. In the ``a.y`` lookup, the dot operator |
| 75 | finds a descriptor instance, recognized by its ``__get__`` method, and calls |
| 76 | that method which returns ``10``. |
| 77 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 78 | Note that the value ``10`` is not stored in either the class dictionary or the |
| 79 | instance dictionary. Instead, the value ``10`` is computed on demand. |
| 80 | |
| 81 | This example shows how a simple descriptor works, but it isn't very useful. |
| 82 | For retrieving constants, normal attribute lookup would be better. |
| 83 | |
| 84 | In the next section, we'll create something more useful, a dynamic lookup. |
| 85 | |
| 86 | |
| 87 | Dynamic lookups |
| 88 | --------------- |
| 89 | |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 90 | Interesting descriptors typically run computations instead of returning |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 91 | constants: |
| 92 | |
| 93 | .. testcode:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 94 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 95 | import os |
| 96 | |
| 97 | class DirectorySize: |
| 98 | |
| 99 | def __get__(self, obj, objtype=None): |
| 100 | return len(os.listdir(obj.dirname)) |
| 101 | |
| 102 | class Directory: |
| 103 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 104 | size = DirectorySize() # Descriptor instance |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 105 | |
| 106 | def __init__(self, dirname): |
| 107 | self.dirname = dirname # Regular instance attribute |
| 108 | |
| 109 | An interactive session shows that the lookup is dynamic — it computes |
| 110 | different, updated answers each time:: |
| 111 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 112 | >>> s = Directory('songs') |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 113 | >>> g = Directory('games') |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 114 | >>> s.size # The songs directory has twenty files |
| 115 | 20 |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 116 | >>> g.size # The games directory has three files |
| 117 | 3 |
| 118 | >>> open('games/newfile').close() # Add a fourth file to the directory |
| 119 | >>> g.size # File count is automatically updated |
| 120 | 4 |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 121 | |
| 122 | Besides showing how descriptors can run computations, this example also |
| 123 | reveals the purpose of the parameters to :meth:`__get__`. The *self* |
| 124 | parameter is *size*, an instance of *DirectorySize*. The *obj* parameter is |
Raymond Hettinger | 8031877 | 2020-11-06 01:30:17 -0800 | [diff] [blame] | 125 | either *g* or *s*, an instance of *Directory*. It is the *obj* parameter that |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 126 | lets the :meth:`__get__` method learn the target directory. The *objtype* |
| 127 | parameter is the class *Directory*. |
| 128 | |
| 129 | |
| 130 | Managed attributes |
| 131 | ------------------ |
| 132 | |
| 133 | A popular use for descriptors is managing access to instance data. The |
| 134 | descriptor is assigned to a public attribute in the class dictionary while the |
| 135 | actual data is stored as a private attribute in the instance dictionary. The |
| 136 | descriptor's :meth:`__get__` and :meth:`__set__` methods are triggered when |
| 137 | the public attribute is accessed. |
| 138 | |
| 139 | In the following example, *age* is the public attribute and *_age* is the |
| 140 | private attribute. When the public attribute is accessed, the descriptor logs |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 141 | the lookup or update: |
| 142 | |
| 143 | .. testcode:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 144 | |
| 145 | import logging |
| 146 | |
| 147 | logging.basicConfig(level=logging.INFO) |
| 148 | |
| 149 | class LoggedAgeAccess: |
| 150 | |
| 151 | def __get__(self, obj, objtype=None): |
| 152 | value = obj._age |
| 153 | logging.info('Accessing %r giving %r', 'age', value) |
| 154 | return value |
| 155 | |
| 156 | def __set__(self, obj, value): |
| 157 | logging.info('Updating %r to %r', 'age', value) |
| 158 | obj._age = value |
| 159 | |
| 160 | class Person: |
| 161 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 162 | age = LoggedAgeAccess() # Descriptor instance |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 163 | |
| 164 | def __init__(self, name, age): |
| 165 | self.name = name # Regular instance attribute |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 166 | self.age = age # Calls __set__() |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 167 | |
| 168 | def birthday(self): |
| 169 | self.age += 1 # Calls both __get__() and __set__() |
| 170 | |
| 171 | |
| 172 | An interactive session shows that all access to the managed attribute *age* is |
| 173 | logged, but that the regular attribute *name* is not logged:: |
| 174 | |
| 175 | >>> mary = Person('Mary M', 30) # The initial age update is logged |
| 176 | INFO:root:Updating 'age' to 30 |
| 177 | >>> dave = Person('David D', 40) |
| 178 | INFO:root:Updating 'age' to 40 |
| 179 | |
| 180 | >>> vars(mary) # The actual data is in a private attribute |
| 181 | {'name': 'Mary M', '_age': 30} |
| 182 | >>> vars(dave) |
| 183 | {'name': 'David D', '_age': 40} |
| 184 | |
| 185 | >>> mary.age # Access the data and log the lookup |
| 186 | INFO:root:Accessing 'age' giving 30 |
| 187 | 30 |
| 188 | >>> mary.birthday() # Updates are logged as well |
| 189 | INFO:root:Accessing 'age' giving 30 |
| 190 | INFO:root:Updating 'age' to 31 |
| 191 | |
| 192 | >>> dave.name # Regular attribute lookup isn't logged |
| 193 | 'David D' |
| 194 | >>> dave.age # Only the managed attribute is logged |
| 195 | INFO:root:Accessing 'age' giving 40 |
| 196 | 40 |
| 197 | |
Raymond Hettinger | 8031877 | 2020-11-06 01:30:17 -0800 | [diff] [blame] | 198 | One major issue with this example is that the private name *_age* is hardwired in |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 199 | the *LoggedAgeAccess* class. That means that each instance can only have one |
| 200 | logged attribute and that its name is unchangeable. In the next example, |
| 201 | we'll fix that problem. |
| 202 | |
| 203 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 204 | Customized names |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 205 | ---------------- |
| 206 | |
Raymond Hettinger | 8031877 | 2020-11-06 01:30:17 -0800 | [diff] [blame] | 207 | When a class uses descriptors, it can inform each descriptor about which |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 208 | variable name was used. |
| 209 | |
| 210 | In this example, the :class:`Person` class has two descriptor instances, |
| 211 | *name* and *age*. When the :class:`Person` class is defined, it makes a |
| 212 | callback to :meth:`__set_name__` in *LoggedAccess* so that the field names can |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 213 | be recorded, giving each descriptor its own *public_name* and *private_name*: |
| 214 | |
| 215 | .. testcode:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 216 | |
| 217 | import logging |
| 218 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 219 | logging.basicConfig(level=logging.INFO) |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 220 | |
| 221 | class LoggedAccess: |
| 222 | |
| 223 | def __set_name__(self, owner, name): |
| 224 | self.public_name = name |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 225 | self.private_name = '_' + name |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 226 | |
| 227 | def __get__(self, obj, objtype=None): |
| 228 | value = getattr(obj, self.private_name) |
| 229 | logging.info('Accessing %r giving %r', self.public_name, value) |
| 230 | return value |
| 231 | |
| 232 | def __set__(self, obj, value): |
| 233 | logging.info('Updating %r to %r', self.public_name, value) |
| 234 | setattr(obj, self.private_name, value) |
| 235 | |
| 236 | class Person: |
| 237 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 238 | name = LoggedAccess() # First descriptor instance |
| 239 | age = LoggedAccess() # Second descriptor instance |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 240 | |
| 241 | def __init__(self, name, age): |
| 242 | self.name = name # Calls the first descriptor |
| 243 | self.age = age # Calls the second descriptor |
| 244 | |
| 245 | def birthday(self): |
| 246 | self.age += 1 |
| 247 | |
| 248 | An interactive session shows that the :class:`Person` class has called |
| 249 | :meth:`__set_name__` so that the field names would be recorded. Here |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 250 | we call :func:`vars` to look up the descriptor without triggering it: |
| 251 | |
| 252 | .. doctest:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 253 | |
| 254 | >>> vars(vars(Person)['name']) |
| 255 | {'public_name': 'name', 'private_name': '_name'} |
| 256 | >>> vars(vars(Person)['age']) |
| 257 | {'public_name': 'age', 'private_name': '_age'} |
| 258 | |
| 259 | The new class now logs access to both *name* and *age*:: |
| 260 | |
| 261 | >>> pete = Person('Peter P', 10) |
| 262 | INFO:root:Updating 'name' to 'Peter P' |
| 263 | INFO:root:Updating 'age' to 10 |
| 264 | >>> kate = Person('Catherine C', 20) |
| 265 | INFO:root:Updating 'name' to 'Catherine C' |
| 266 | INFO:root:Updating 'age' to 20 |
| 267 | |
| 268 | The two *Person* instances contain only the private names:: |
| 269 | |
| 270 | >>> vars(pete) |
| 271 | {'_name': 'Peter P', '_age': 10} |
| 272 | >>> vars(kate) |
| 273 | {'_name': 'Catherine C', '_age': 20} |
| 274 | |
| 275 | |
| 276 | Closing thoughts |
| 277 | ---------------- |
| 278 | |
| 279 | A :term:`descriptor` is what we call any object that defines :meth:`__get__`, |
| 280 | :meth:`__set__`, or :meth:`__delete__`. |
| 281 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 282 | Optionally, descriptors can have a :meth:`__set_name__` method. This is only |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 283 | used in cases where a descriptor needs to know either the class where it was |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 284 | created or the name of class variable it was assigned to. (This method, if |
| 285 | present, is called even if the class is not a descriptor.) |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 286 | |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 287 | Descriptors get invoked by the dot "operator" during attribute lookup. If a |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 288 | descriptor is accessed indirectly with ``vars(some_class)[descriptor_name]``, |
| 289 | the descriptor instance is returned without invoking it. |
| 290 | |
| 291 | Descriptors only work when used as class variables. When put in instances, |
| 292 | they have no effect. |
| 293 | |
| 294 | The main motivation for descriptors is to provide a hook allowing objects |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 295 | stored in class variables to control what happens during attribute lookup. |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 296 | |
| 297 | Traditionally, the calling class controls what happens during lookup. |
| 298 | Descriptors invert that relationship and allow the data being looked-up to |
| 299 | have a say in the matter. |
| 300 | |
| 301 | Descriptors are used throughout the language. It is how functions turn into |
| 302 | bound methods. Common tools like :func:`classmethod`, :func:`staticmethod`, |
| 303 | :func:`property`, and :func:`functools.cached_property` are all implemented as |
| 304 | descriptors. |
| 305 | |
| 306 | |
| 307 | Complete Practical Example |
| 308 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 309 | |
| 310 | In this example, we create a practical and powerful tool for locating |
| 311 | notoriously hard to find data corruption bugs. |
| 312 | |
| 313 | |
| 314 | Validator class |
| 315 | --------------- |
| 316 | |
| 317 | A validator is a descriptor for managed attribute access. Prior to storing |
| 318 | any data, it verifies that the new value meets various type and range |
| 319 | restrictions. If those restrictions aren't met, it raises an exception to |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 320 | prevent data corruption at its source. |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 321 | |
| 322 | This :class:`Validator` class is both an :term:`abstract base class` and a |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 323 | managed attribute descriptor: |
| 324 | |
| 325 | .. testcode:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 326 | |
| 327 | from abc import ABC, abstractmethod |
| 328 | |
| 329 | class Validator(ABC): |
| 330 | |
| 331 | def __set_name__(self, owner, name): |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 332 | self.private_name = '_' + name |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 333 | |
| 334 | def __get__(self, obj, objtype=None): |
| 335 | return getattr(obj, self.private_name) |
| 336 | |
| 337 | def __set__(self, obj, value): |
| 338 | self.validate(value) |
| 339 | setattr(obj, self.private_name, value) |
| 340 | |
| 341 | @abstractmethod |
| 342 | def validate(self, value): |
| 343 | pass |
| 344 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 345 | Custom validators need to inherit from :class:`Validator` and must supply a |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 346 | :meth:`validate` method to test various restrictions as needed. |
| 347 | |
| 348 | |
| 349 | Custom validators |
| 350 | ----------------- |
| 351 | |
| 352 | Here are three practical data validation utilities: |
| 353 | |
| 354 | 1) :class:`OneOf` verifies that a value is one of a restricted set of options. |
| 355 | |
| 356 | 2) :class:`Number` verifies that a value is either an :class:`int` or |
| 357 | :class:`float`. Optionally, it verifies that a value is between a given |
| 358 | minimum or maximum. |
| 359 | |
| 360 | 3) :class:`String` verifies that a value is a :class:`str`. Optionally, it |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 361 | validates a given minimum or maximum length. It can validate a |
| 362 | user-defined `predicate |
| 363 | <https://en.wikipedia.org/wiki/Predicate_(mathematical_logic)>`_ as well. |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 364 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 365 | .. testcode:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 366 | |
| 367 | class OneOf(Validator): |
| 368 | |
| 369 | def __init__(self, *options): |
| 370 | self.options = set(options) |
| 371 | |
| 372 | def validate(self, value): |
| 373 | if value not in self.options: |
| 374 | raise ValueError(f'Expected {value!r} to be one of {self.options!r}') |
| 375 | |
| 376 | class Number(Validator): |
| 377 | |
| 378 | def __init__(self, minvalue=None, maxvalue=None): |
| 379 | self.minvalue = minvalue |
| 380 | self.maxvalue = maxvalue |
| 381 | |
| 382 | def validate(self, value): |
| 383 | if not isinstance(value, (int, float)): |
| 384 | raise TypeError(f'Expected {value!r} to be an int or float') |
| 385 | if self.minvalue is not None and value < self.minvalue: |
| 386 | raise ValueError( |
| 387 | f'Expected {value!r} to be at least {self.minvalue!r}' |
| 388 | ) |
| 389 | if self.maxvalue is not None and value > self.maxvalue: |
| 390 | raise ValueError( |
| 391 | f'Expected {value!r} to be no more than {self.maxvalue!r}' |
| 392 | ) |
| 393 | |
| 394 | class String(Validator): |
| 395 | |
| 396 | def __init__(self, minsize=None, maxsize=None, predicate=None): |
| 397 | self.minsize = minsize |
| 398 | self.maxsize = maxsize |
| 399 | self.predicate = predicate |
| 400 | |
| 401 | def validate(self, value): |
| 402 | if not isinstance(value, str): |
| 403 | raise TypeError(f'Expected {value!r} to be an str') |
| 404 | if self.minsize is not None and len(value) < self.minsize: |
| 405 | raise ValueError( |
| 406 | f'Expected {value!r} to be no smaller than {self.minsize!r}' |
| 407 | ) |
| 408 | if self.maxsize is not None and len(value) > self.maxsize: |
| 409 | raise ValueError( |
| 410 | f'Expected {value!r} to be no bigger than {self.maxsize!r}' |
| 411 | ) |
| 412 | if self.predicate is not None and not self.predicate(value): |
| 413 | raise ValueError( |
| 414 | f'Expected {self.predicate} to be true for {value!r}' |
| 415 | ) |
| 416 | |
| 417 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 418 | Practical application |
| 419 | --------------------- |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 420 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 421 | Here's how the data validators can be used in a real class: |
| 422 | |
| 423 | .. testcode:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 424 | |
| 425 | class Component: |
| 426 | |
| 427 | name = String(minsize=3, maxsize=10, predicate=str.isupper) |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 428 | kind = OneOf('wood', 'metal', 'plastic') |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 429 | quantity = Number(minvalue=0) |
| 430 | |
| 431 | def __init__(self, name, kind, quantity): |
| 432 | self.name = name |
| 433 | self.kind = kind |
| 434 | self.quantity = quantity |
| 435 | |
| 436 | The descriptors prevent invalid instances from being created:: |
| 437 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 438 | >>> Component('Widget', 'metal', 5) # Blocked: 'Widget' is not all uppercase |
| 439 | Traceback (most recent call last): |
| 440 | ... |
| 441 | ValueError: Expected <method 'isupper' of 'str' objects> to be true for 'Widget' |
| 442 | |
| 443 | >>> Component('WIDGET', 'metle', 5) # Blocked: 'metle' is misspelled |
| 444 | Traceback (most recent call last): |
| 445 | ... |
| 446 | ValueError: Expected 'metle' to be one of {'metal', 'plastic', 'wood'} |
| 447 | |
| 448 | >>> Component('WIDGET', 'metal', -5) # Blocked: -5 is negative |
| 449 | Traceback (most recent call last): |
| 450 | ... |
| 451 | ValueError: Expected -5 to be at least 0 |
| 452 | >>> Component('WIDGET', 'metal', 'V') # Blocked: 'V' isn't a number |
| 453 | Traceback (most recent call last): |
| 454 | ... |
| 455 | TypeError: Expected 'V' to be an int or float |
| 456 | |
| 457 | >>> c = Component('WIDGET', 'metal', 5) # Allowed: The inputs are valid |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 458 | |
| 459 | |
| 460 | Technical Tutorial |
| 461 | ^^^^^^^^^^^^^^^^^^ |
| 462 | |
| 463 | What follows is a more technical tutorial for the mechanics and details of how |
| 464 | descriptors work. |
| 465 | |
| 466 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 467 | Abstract |
| 468 | -------- |
| 469 | |
| 470 | Defines descriptors, summarizes the protocol, and shows how descriptors are |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 471 | called. Provides an example showing how object relational mappings work. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 472 | |
| 473 | Learning about descriptors not only provides access to a larger toolset, it |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 474 | creates a deeper understanding of how Python works. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 475 | |
| 476 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 477 | Definition and introduction |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 478 | --------------------------- |
| 479 | |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 480 | In general, a descriptor is an attribute value that has one of the methods in |
| 481 | the descriptor protocol. Those methods are :meth:`__get__`, :meth:`__set__`, |
| 482 | and :meth:`__delete__`. If any of those methods are defined for an the |
| 483 | attribute, it is said to be a :term:`descriptor`. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 484 | |
| 485 | The default behavior for attribute access is to get, set, or delete the |
| 486 | attribute from an object's dictionary. For instance, ``a.x`` has a lookup chain |
| 487 | starting with ``a.__dict__['x']``, then ``type(a).__dict__['x']``, and |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 488 | continuing through the method resolution order of ``type(a)``. If the |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 489 | looked-up value is an object defining one of the descriptor methods, then Python |
| 490 | may override the default behavior and invoke the descriptor method instead. |
| 491 | Where this occurs in the precedence chain depends on which descriptor methods |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 492 | were defined. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 493 | |
| 494 | Descriptors are a powerful, general purpose protocol. They are the mechanism |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 495 | behind properties, methods, static methods, class methods, and |
| 496 | :func:`super()`. They are used throughout Python itself. Descriptors |
| 497 | simplify the underlying C code and offer a flexible set of new tools for |
| 498 | everyday Python programs. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 499 | |
| 500 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 501 | Descriptor protocol |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 502 | ------------------- |
| 503 | |
NotAFile | 28ea4c2 | 2018-09-10 23:35:38 +0200 | [diff] [blame] | 504 | ``descr.__get__(self, obj, type=None) -> value`` |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 505 | |
NotAFile | 28ea4c2 | 2018-09-10 23:35:38 +0200 | [diff] [blame] | 506 | ``descr.__set__(self, obj, value) -> None`` |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 507 | |
NotAFile | 28ea4c2 | 2018-09-10 23:35:38 +0200 | [diff] [blame] | 508 | ``descr.__delete__(self, obj) -> None`` |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 509 | |
| 510 | That is all there is to it. Define any of these methods and an object is |
| 511 | considered a descriptor and can override default behavior upon being looked up |
| 512 | as an attribute. |
| 513 | |
Aaron Hall, MBA | 4054b17 | 2018-05-20 19:46:42 -0400 | [diff] [blame] | 514 | If an object defines :meth:`__set__` or :meth:`__delete__`, it is considered |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 515 | a data descriptor. Descriptors that only define :meth:`__get__` are called |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 516 | non-data descriptors (they are often used for methods but other uses are |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 517 | possible). |
| 518 | |
| 519 | Data and non-data descriptors differ in how overrides are calculated with |
| 520 | respect to entries in an instance's dictionary. If an instance's dictionary |
| 521 | has an entry with the same name as a data descriptor, the data descriptor |
| 522 | takes precedence. If an instance's dictionary has an entry with the same |
| 523 | name as a non-data descriptor, the dictionary entry takes precedence. |
| 524 | |
| 525 | To make a read-only data descriptor, define both :meth:`__get__` and |
| 526 | :meth:`__set__` with the :meth:`__set__` raising an :exc:`AttributeError` when |
| 527 | called. Defining the :meth:`__set__` method with an exception raising |
| 528 | placeholder is enough to make it a data descriptor. |
| 529 | |
| 530 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 531 | Overview of descriptor invocation |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 532 | --------------------------------- |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 533 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 534 | A descriptor can be called directly with ``desc.__get__(obj)`` or |
| 535 | ``desc.__get__(None, cls)``. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 536 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 537 | But it is more common for a descriptor to be invoked automatically from |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 538 | attribute access. |
| 539 | |
| 540 | The expression ``obj.x`` looks up the attribute ``x`` in the chain of |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 541 | namespaces for ``obj``. If the search finds a descriptor outside of the |
| 542 | instance ``__dict__``, its :meth:`__get__` method is invoked according to the |
| 543 | precedence rules listed below. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 544 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 545 | The details of invocation depend on whether ``obj`` is an object, class, or |
| 546 | instance of super. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 547 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 548 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 549 | Invocation from an instance |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 550 | --------------------------- |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 551 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 552 | Instance lookup scans through a chain of namespaces giving data descriptors |
| 553 | the highest priority, followed by instance variables, then non-data |
| 554 | descriptors, then class variables, and lastly :meth:`__getattr__` if it is |
| 555 | provided. |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 556 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 557 | If a descriptor is found for ``a.x``, then it is invoked with: |
| 558 | ``desc.__get__(a, type(a))``. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 559 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 560 | The logic for a dotted lookup is in :meth:`object.__getattribute__`. Here is |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 561 | a pure Python equivalent: |
| 562 | |
| 563 | .. testcode:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 564 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 565 | def object_getattribute(obj, name): |
| 566 | "Emulate PyObject_GenericGetAttr() in Objects/object.c" |
| 567 | null = object() |
| 568 | objtype = type(obj) |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 569 | cls_var = getattr(objtype, name, null) |
| 570 | descr_get = getattr(type(cls_var), '__get__', null) |
| 571 | if descr_get is not null: |
| 572 | if (hasattr(type(cls_var), '__set__') |
| 573 | or hasattr(type(cls_var), '__delete__')): |
| 574 | return descr_get(cls_var, obj, objtype) # data descriptor |
| 575 | if hasattr(obj, '__dict__') and name in vars(obj): |
| 576 | return vars(obj)[name] # instance variable |
| 577 | if descr_get is not null: |
| 578 | return descr_get(cls_var, obj, objtype) # non-data descriptor |
| 579 | if cls_var is not null: |
| 580 | return cls_var # class variable |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 581 | raise AttributeError(name) |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 582 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 583 | |
| 584 | .. testcode:: |
| 585 | :hide: |
| 586 | |
| 587 | # Test the fidelity of object_getattribute() by comparing it with the |
| 588 | # normal object.__getattribute__(). The former will be accessed by |
| 589 | # square brackets and the latter by the dot operator. |
| 590 | |
| 591 | class Object: |
| 592 | |
| 593 | def __getitem__(obj, name): |
| 594 | try: |
| 595 | return object_getattribute(obj, name) |
| 596 | except AttributeError: |
| 597 | if not hasattr(type(obj), '__getattr__'): |
| 598 | raise |
| 599 | return type(obj).__getattr__(obj, name) # __getattr__ |
| 600 | |
| 601 | class DualOperator(Object): |
| 602 | |
| 603 | x = 10 |
| 604 | |
| 605 | def __init__(self, z): |
| 606 | self.z = z |
| 607 | |
| 608 | @property |
| 609 | def p2(self): |
| 610 | return 2 * self.x |
| 611 | |
| 612 | @property |
| 613 | def p3(self): |
| 614 | return 3 * self.x |
| 615 | |
| 616 | def m5(self, y): |
| 617 | return 5 * y |
| 618 | |
| 619 | def m7(self, y): |
| 620 | return 7 * y |
| 621 | |
| 622 | def __getattr__(self, name): |
| 623 | return ('getattr_hook', self, name) |
| 624 | |
| 625 | class DualOperatorWithSlots: |
| 626 | |
| 627 | __getitem__ = Object.__getitem__ |
| 628 | |
| 629 | __slots__ = ['z'] |
| 630 | |
| 631 | x = 15 |
| 632 | |
| 633 | def __init__(self, z): |
| 634 | self.z = z |
| 635 | |
| 636 | @property |
| 637 | def p2(self): |
| 638 | return 2 * self.x |
| 639 | |
| 640 | def m5(self, y): |
| 641 | return 5 * y |
| 642 | |
| 643 | def __getattr__(self, name): |
| 644 | return ('getattr_hook', self, name) |
| 645 | |
| 646 | |
| 647 | .. doctest:: |
| 648 | :hide: |
| 649 | |
| 650 | >>> a = DualOperator(11) |
| 651 | >>> vars(a).update(p3 = '_p3', m7 = '_m7') |
| 652 | >>> a.x == a['x'] == 10 |
| 653 | True |
| 654 | >>> a.z == a['z'] == 11 |
| 655 | True |
| 656 | >>> a.p2 == a['p2'] == 20 |
| 657 | True |
| 658 | >>> a.p3 == a['p3'] == 30 |
| 659 | True |
| 660 | >>> a.m5(100) == a.m5(100) == 500 |
| 661 | True |
| 662 | >>> a.m7 == a['m7'] == '_m7' |
| 663 | True |
| 664 | >>> a.g == a['g'] == ('getattr_hook', a, 'g') |
| 665 | True |
| 666 | |
| 667 | >>> b = DualOperatorWithSlots(22) |
| 668 | >>> b.x == b['x'] == 15 |
| 669 | True |
| 670 | >>> b.z == b['z'] == 22 |
| 671 | True |
| 672 | >>> b.p2 == b['p2'] == 30 |
| 673 | True |
| 674 | >>> b.m5(200) == b['m5'](200) == 1000 |
| 675 | True |
| 676 | >>> b.g == b['g'] == ('getattr_hook', b, 'g') |
| 677 | True |
| 678 | |
| 679 | |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 680 | Interestingly, attribute lookup doesn't call :meth:`object.__getattribute__` |
| 681 | directly. Instead, both the dot operator and the :func:`getattr` function |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 682 | perform attribute lookup by way of a helper function: |
| 683 | |
| 684 | .. testcode:: |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 685 | |
| 686 | def getattr_hook(obj, name): |
| 687 | "Emulate slot_tp_getattr_hook() in Objects/typeobject.c" |
| 688 | try: |
| 689 | return obj.__getattribute__(name) |
| 690 | except AttributeError: |
| 691 | if not hasattr(type(obj), '__getattr__'): |
| 692 | raise |
| 693 | return type(obj).__getattr__(obj, name) # __getattr__ |
| 694 | |
| 695 | So if :meth:`__getattr__` exists, it is called whenever :meth:`__getattribute__` |
| 696 | raises :exc:`AttributeError` (either directly or in one of the descriptor calls). |
| 697 | |
| 698 | Also, if a user calls :meth:`object.__getattribute__` directly, the |
| 699 | :meth:`__getattr__` hook is bypassed entirely. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 700 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 701 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 702 | Invocation from a class |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 703 | ----------------------- |
| 704 | |
| 705 | The logic for a dotted lookup such as ``A.x`` is in |
| 706 | :meth:`type.__getattribute__`. The steps are similar to those for |
| 707 | :meth:`object.__getattribute__` but the instance dictionary lookup is replaced |
| 708 | by a search through the class's :term:`method resolution order`. |
| 709 | |
| 710 | If a descriptor is found, it is invoked with ``desc.__get__(None, A)``. |
| 711 | |
| 712 | The full C implementation can be found in :c:func:`type_getattro()` and |
| 713 | :c:func:`_PyType_Lookup()` in :source:`Objects/typeobject.c`. |
| 714 | |
| 715 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 716 | Invocation from super |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 717 | --------------------- |
| 718 | |
| 719 | The logic for super's dotted lookup is in the :meth:`__getattribute__` method for |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 720 | object returned by :class:`super()`. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 721 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 722 | A dotted lookup such as ``super(A, obj).m`` searches ``obj.__class__.__mro__`` |
| 723 | for the base class ``B`` immediately following ``A`` and then returns |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 724 | ``B.__dict__['m'].__get__(obj, A)``. If not a descriptor, ``m`` is returned |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 725 | unchanged. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 726 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 727 | The full C implementation can be found in :c:func:`super_getattro()` in |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 728 | :source:`Objects/typeobject.c`. A pure Python equivalent can be found in |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 729 | `Guido's Tutorial |
| 730 | <https://www.python.org/download/releases/2.2.3/descrintro/#cooperation>`_. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 731 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 732 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 733 | Summary of invocation logic |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 734 | --------------------------- |
| 735 | |
| 736 | The mechanism for descriptors is embedded in the :meth:`__getattribute__()` |
| 737 | methods for :class:`object`, :class:`type`, and :func:`super`. |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 738 | |
| 739 | The important points to remember are: |
| 740 | |
| 741 | * Descriptors are invoked by the :meth:`__getattribute__` method. |
| 742 | |
| 743 | * Classes inherit this machinery from :class:`object`, :class:`type`, or |
| 744 | :func:`super`. |
| 745 | |
| 746 | * Overriding :meth:`__getattribute__` prevents automatic descriptor calls |
| 747 | because all the descriptor logic is in that method. |
| 748 | |
| 749 | * :meth:`object.__getattribute__` and :meth:`type.__getattribute__` make |
| 750 | different calls to :meth:`__get__`. The first includes the instance and may |
| 751 | include the class. The second puts in ``None`` for the instance and always |
| 752 | includes the class. |
| 753 | |
| 754 | * Data descriptors always override instance dictionaries. |
| 755 | |
| 756 | * Non-data descriptors may be overridden by instance dictionaries. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 757 | |
| 758 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 759 | Automatic name notification |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 760 | --------------------------- |
| 761 | |
| 762 | Sometimes it is desirable for a descriptor to know what class variable name it |
| 763 | was assigned to. When a new class is created, the :class:`type` metaclass |
| 764 | scans the dictionary of the new class. If any of the entries are descriptors |
| 765 | and if they define :meth:`__set_name__`, that method is called with two |
Raymond Hettinger | 8031877 | 2020-11-06 01:30:17 -0800 | [diff] [blame] | 766 | arguments. The *owner* is the class where the descriptor is used, and the |
| 767 | *name* is the class variable the descriptor was assigned to. |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 768 | |
| 769 | The implementation details are in :c:func:`type_new()` and |
| 770 | :c:func:`set_names()` in :source:`Objects/typeobject.c`. |
| 771 | |
| 772 | Since the update logic is in :meth:`type.__new__`, notifications only take |
| 773 | place at the time of class creation. If descriptors are added to the class |
| 774 | afterwards, :meth:`__set_name__` will need to be called manually. |
| 775 | |
| 776 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 777 | ORM example |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 778 | ----------- |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 779 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 780 | The following code is simplified skeleton showing how data descriptors could |
| 781 | be used to implement an `object relational mapping |
| 782 | <https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping>`_. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 783 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 784 | The essential idea is that the data is stored in an external database. The |
| 785 | Python instances only hold keys to the database's tables. Descriptors take |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 786 | care of lookups or updates: |
| 787 | |
| 788 | .. testcode:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 789 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 790 | class Field: |
| 791 | |
| 792 | def __set_name__(self, owner, name): |
| 793 | self.fetch = f'SELECT {name} FROM {owner.table} WHERE {owner.key}=?;' |
| 794 | self.store = f'UPDATE {owner.table} SET {name}=? WHERE {owner.key}=?;' |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 795 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 796 | def __get__(self, obj, objtype=None): |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 797 | return conn.execute(self.fetch, [obj.key]).fetchone()[0] |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 798 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 799 | def __set__(self, obj, value): |
| 800 | conn.execute(self.store, [value, obj.key]) |
| 801 | conn.commit() |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 802 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 803 | We can use the :class:`Field` class to define `models |
| 804 | <https://en.wikipedia.org/wiki/Database_model>`_ that describe the schema for |
| 805 | each table in a database: |
| 806 | |
| 807 | .. testcode:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 808 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 809 | class Movie: |
| 810 | table = 'Movies' # Table name |
| 811 | key = 'title' # Primary key |
| 812 | director = Field() |
| 813 | year = Field() |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 814 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 815 | def __init__(self, key): |
| 816 | self.key = key |
| 817 | |
| 818 | class Song: |
| 819 | table = 'Music' |
| 820 | key = 'title' |
| 821 | artist = Field() |
| 822 | year = Field() |
| 823 | genre = Field() |
| 824 | |
| 825 | def __init__(self, key): |
| 826 | self.key = key |
| 827 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 828 | To use the models, first connect to the database:: |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 829 | |
| 830 | >>> import sqlite3 |
| 831 | >>> conn = sqlite3.connect('entertainment.db') |
| 832 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 833 | An interactive session shows how data is retrieved from the database and how |
| 834 | it can be updated: |
| 835 | |
| 836 | .. testsetup:: |
| 837 | |
| 838 | song_data = [ |
| 839 | ('Country Roads', 'John Denver', 1972), |
| 840 | ('Me and Bobby McGee', 'Janice Joplin', 1971), |
| 841 | ('Coal Miners Daughter', 'Loretta Lynn', 1970), |
| 842 | ] |
| 843 | |
| 844 | movie_data = [ |
| 845 | ('Star Wars', 'George Lucas', 1977), |
| 846 | ('Jaws', 'Steven Spielberg', 1975), |
| 847 | ('Aliens', 'James Cameron', 1986), |
| 848 | ] |
| 849 | |
| 850 | import sqlite3 |
| 851 | |
| 852 | conn = sqlite3.connect(':memory:') |
| 853 | conn.execute('CREATE TABLE Music (title text, artist text, year integer);') |
| 854 | conn.execute('CREATE INDEX MusicNdx ON Music (title);') |
| 855 | conn.executemany('INSERT INTO Music VALUES (?, ?, ?);', song_data) |
| 856 | conn.execute('CREATE TABLE Movies (title text, director text, year integer);') |
| 857 | conn.execute('CREATE INDEX MovieNdx ON Music (title);') |
| 858 | conn.executemany('INSERT INTO Movies VALUES (?, ?, ?);', movie_data) |
| 859 | conn.commit() |
| 860 | |
| 861 | .. doctest:: |
| 862 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 863 | >>> Movie('Star Wars').director |
| 864 | 'George Lucas' |
| 865 | >>> jaws = Movie('Jaws') |
| 866 | >>> f'Released in {jaws.year} by {jaws.director}' |
| 867 | 'Released in 1975 by Steven Spielberg' |
| 868 | |
| 869 | >>> Song('Country Roads').artist |
| 870 | 'John Denver' |
| 871 | |
| 872 | >>> Movie('Star Wars').director = 'J.J. Abrams' |
| 873 | >>> Movie('Star Wars').director |
| 874 | 'J.J. Abrams' |
| 875 | |
Raymond Hettinger | c272d40 | 2020-11-15 17:44:28 -0800 | [diff] [blame] | 876 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 877 | Pure Python Equivalents |
| 878 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 879 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 880 | The descriptor protocol is simple and offers exciting possibilities. Several |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame] | 881 | use cases are so common that they have been prepackaged into built-in tools. |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 882 | Properties, bound methods, static methods, class methods, and \_\_slots\_\_ are |
| 883 | all based on the descriptor protocol. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 884 | |
| 885 | |
| 886 | Properties |
| 887 | ---------- |
| 888 | |
| 889 | Calling :func:`property` is a succinct way of building a data descriptor that |
Raymond Hettinger | 8031877 | 2020-11-06 01:30:17 -0800 | [diff] [blame] | 890 | triggers a function call upon access to an attribute. Its signature is:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 891 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 892 | property(fget=None, fset=None, fdel=None, doc=None) -> property |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 893 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 894 | The documentation shows a typical use to define a managed attribute ``x``: |
| 895 | |
| 896 | .. testcode:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 897 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 898 | class C: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 899 | def getx(self): return self.__x |
| 900 | def setx(self, value): self.__x = value |
| 901 | def delx(self): del self.__x |
| 902 | x = property(getx, setx, delx, "I'm the 'x' property.") |
| 903 | |
| 904 | To see how :func:`property` is implemented in terms of the descriptor protocol, |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 905 | here is a pure Python equivalent: |
| 906 | |
| 907 | .. testcode:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 908 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 909 | class Property: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 910 | "Emulate PyProperty_Type() in Objects/descrobject.c" |
| 911 | |
| 912 | def __init__(self, fget=None, fset=None, fdel=None, doc=None): |
| 913 | self.fget = fget |
| 914 | self.fset = fset |
| 915 | self.fdel = fdel |
Raymond Hettinger | 632c8c8 | 2013-03-10 09:41:18 -0700 | [diff] [blame] | 916 | if doc is None and fget is not None: |
| 917 | doc = fget.__doc__ |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 918 | self.__doc__ = doc |
| 919 | |
| 920 | def __get__(self, obj, objtype=None): |
| 921 | if obj is None: |
| 922 | return self |
| 923 | if self.fget is None: |
Raymond Hettinger | 632c8c8 | 2013-03-10 09:41:18 -0700 | [diff] [blame] | 924 | raise AttributeError("unreadable attribute") |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 925 | return self.fget(obj) |
| 926 | |
| 927 | def __set__(self, obj, value): |
| 928 | if self.fset is None: |
Raymond Hettinger | 632c8c8 | 2013-03-10 09:41:18 -0700 | [diff] [blame] | 929 | raise AttributeError("can't set attribute") |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 930 | self.fset(obj, value) |
| 931 | |
| 932 | def __delete__(self, obj): |
| 933 | if self.fdel is None: |
Raymond Hettinger | 632c8c8 | 2013-03-10 09:41:18 -0700 | [diff] [blame] | 934 | raise AttributeError("can't delete attribute") |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 935 | self.fdel(obj) |
| 936 | |
Raymond Hettinger | 632c8c8 | 2013-03-10 09:41:18 -0700 | [diff] [blame] | 937 | def getter(self, fget): |
| 938 | return type(self)(fget, self.fset, self.fdel, self.__doc__) |
| 939 | |
| 940 | def setter(self, fset): |
| 941 | return type(self)(self.fget, fset, self.fdel, self.__doc__) |
| 942 | |
| 943 | def deleter(self, fdel): |
| 944 | return type(self)(self.fget, self.fset, fdel, self.__doc__) |
| 945 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 946 | .. testcode:: |
| 947 | :hide: |
| 948 | |
| 949 | # Verify the Property() emulation |
| 950 | |
| 951 | class CC: |
| 952 | def getx(self): |
| 953 | return self.__x |
| 954 | def setx(self, value): |
| 955 | self.__x = value |
| 956 | def delx(self): |
| 957 | del self.__x |
| 958 | x = Property(getx, setx, delx, "I'm the 'x' property.") |
| 959 | |
| 960 | # Now do it again but use the decorator style |
| 961 | |
| 962 | class CCC: |
| 963 | @Property |
| 964 | def x(self): |
| 965 | return self.__x |
| 966 | @x.setter |
| 967 | def x(self, value): |
| 968 | self.__x = value |
| 969 | @x.deleter |
| 970 | def x(self): |
| 971 | del self.__x |
| 972 | |
| 973 | |
| 974 | .. doctest:: |
| 975 | :hide: |
| 976 | |
| 977 | >>> cc = CC() |
| 978 | >>> hasattr(cc, 'x') |
| 979 | False |
| 980 | >>> cc.x = 33 |
| 981 | >>> cc.x |
| 982 | 33 |
| 983 | >>> del cc.x |
| 984 | >>> hasattr(cc, 'x') |
| 985 | False |
| 986 | |
| 987 | >>> ccc = CCC() |
| 988 | >>> hasattr(ccc, 'x') |
| 989 | False |
| 990 | >>> ccc.x = 333 |
| 991 | >>> ccc.x == 333 |
| 992 | True |
| 993 | >>> del ccc.x |
| 994 | >>> hasattr(ccc, 'x') |
| 995 | False |
| 996 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 997 | The :func:`property` builtin helps whenever a user interface has granted |
| 998 | attribute access and then subsequent changes require the intervention of a |
| 999 | method. |
| 1000 | |
| 1001 | For instance, a spreadsheet class may grant access to a cell value through |
| 1002 | ``Cell('b10').value``. Subsequent improvements to the program require the cell |
| 1003 | to be recalculated on every access; however, the programmer does not want to |
| 1004 | affect existing client code accessing the attribute directly. The solution is |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1005 | to wrap access to the value attribute in a property data descriptor: |
| 1006 | |
| 1007 | .. testcode:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1008 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 1009 | class Cell: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1010 | ... |
| 1011 | |
| 1012 | @property |
| 1013 | def value(self): |
_ = NaN | b066edf | 2017-06-23 11:54:35 +0800 | [diff] [blame] | 1014 | "Recalculate the cell before returning value" |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1015 | self.recalc() |
_ = NaN | b066edf | 2017-06-23 11:54:35 +0800 | [diff] [blame] | 1016 | return self._value |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1017 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1018 | Either the built-in :func:`property` or our :func:`Property` equivalent would |
| 1019 | work in this example. |
| 1020 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1021 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 1022 | Functions and methods |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1023 | --------------------- |
| 1024 | |
| 1025 | Python's object oriented features are built upon a function based environment. |
| 1026 | Using non-data descriptors, the two are merged seamlessly. |
| 1027 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 1028 | Functions stored in class dictionaries get turned into methods when invoked. |
| 1029 | Methods only differ from regular functions in that the object instance is |
| 1030 | prepended to the other arguments. By convention, the instance is called |
| 1031 | *self* but could be called *this* or any other variable name. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1032 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 1033 | Methods can be created manually with :class:`types.MethodType` which is |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1034 | roughly equivalent to: |
| 1035 | |
| 1036 | .. testcode:: |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 1037 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 1038 | class MethodType: |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 1039 | "Emulate Py_MethodType in Objects/classobject.c" |
| 1040 | |
| 1041 | def __init__(self, func, obj): |
| 1042 | self.__func__ = func |
| 1043 | self.__self__ = obj |
| 1044 | |
| 1045 | def __call__(self, *args, **kwargs): |
| 1046 | func = self.__func__ |
| 1047 | obj = self.__self__ |
| 1048 | return func(obj, *args, **kwargs) |
| 1049 | |
| 1050 | To support automatic creation of methods, functions include the |
| 1051 | :meth:`__get__` method for binding methods during attribute access. This |
Raymond Hettinger | 8031877 | 2020-11-06 01:30:17 -0800 | [diff] [blame] | 1052 | means that functions are non-data descriptors that return bound methods |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1053 | during dotted lookup from an instance. Here's how it works: |
| 1054 | |
| 1055 | .. testcode:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1056 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 1057 | class Function: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1058 | ... |
| 1059 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1060 | def __get__(self, obj, objtype=None): |
| 1061 | "Simulate func_descr_get() in Objects/funcobject.c" |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 1062 | if obj is None: |
| 1063 | return self |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 1064 | return MethodType(self, obj) |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1065 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 1066 | Running the following class in the interpreter shows how the function |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1067 | descriptor works in practice: |
| 1068 | |
| 1069 | .. testcode:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1070 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1071 | class D: |
| 1072 | def f(self, x): |
| 1073 | return x |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 1074 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1075 | The function has a :term:`qualified name` attribute to support introspection: |
| 1076 | |
| 1077 | .. doctest:: |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 1078 | |
| 1079 | >>> D.f.__qualname__ |
| 1080 | 'D.f' |
| 1081 | |
| 1082 | Accessing the function through the class dictionary does not invoke |
| 1083 | :meth:`__get__`. Instead, it just returns the underlying function object:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1084 | |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 1085 | >>> D.__dict__['f'] |
| 1086 | <function D.f at 0x00C45070> |
| 1087 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1088 | Dotted access from a class calls :meth:`__get__` which just returns the |
| 1089 | underlying function unchanged:: |
| 1090 | |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 1091 | >>> D.f |
| 1092 | <function D.f at 0x00C45070> |
| 1093 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 1094 | The interesting behavior occurs during dotted access from an instance. The |
| 1095 | dotted lookup calls :meth:`__get__` which returns a bound method object:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1096 | |
| 1097 | >>> d = D() |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 1098 | >>> d.f |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1099 | <bound method D.f of <__main__.D object at 0x00B18C90>> |
| 1100 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1101 | Internally, the bound method stores the underlying function and the bound |
| 1102 | instance:: |
| 1103 | |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 1104 | >>> d.f.__func__ |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1105 | <function D.f at 0x00C45070> |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 1106 | |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 1107 | >>> d.f.__self__ |
| 1108 | <__main__.D object at 0x1012e1f98> |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1109 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 1110 | If you have ever wondered where *self* comes from in regular methods or where |
| 1111 | *cls* comes from in class methods, this is it! |
| 1112 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1113 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 1114 | Static methods |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 1115 | -------------- |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1116 | |
| 1117 | Non-data descriptors provide a simple mechanism for variations on the usual |
| 1118 | patterns of binding functions into methods. |
| 1119 | |
| 1120 | To recap, functions have a :meth:`__get__` method so that they can be converted |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 1121 | to a method when accessed as attributes. The non-data descriptor transforms an |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1122 | ``obj.f(*args)`` call into ``f(obj, *args)``. Calling ``cls.f(*args)`` |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1123 | becomes ``f(*args)``. |
| 1124 | |
| 1125 | This chart summarizes the binding and its two most useful variants: |
| 1126 | |
| 1127 | +-----------------+----------------------+------------------+ |
| 1128 | | Transformation | Called from an | Called from a | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1129 | | | object | class | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1130 | +=================+======================+==================+ |
| 1131 | | function | f(obj, \*args) | f(\*args) | |
| 1132 | +-----------------+----------------------+------------------+ |
| 1133 | | staticmethod | f(\*args) | f(\*args) | |
| 1134 | +-----------------+----------------------+------------------+ |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1135 | | classmethod | f(type(obj), \*args) | f(cls, \*args) | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1136 | +-----------------+----------------------+------------------+ |
| 1137 | |
| 1138 | Static methods return the underlying function without changes. Calling either |
| 1139 | ``c.f`` or ``C.f`` is the equivalent of a direct lookup into |
| 1140 | ``object.__getattribute__(c, "f")`` or ``object.__getattribute__(C, "f")``. As a |
| 1141 | result, the function becomes identically accessible from either an object or a |
| 1142 | class. |
| 1143 | |
| 1144 | Good candidates for static methods are methods that do not reference the |
| 1145 | ``self`` variable. |
| 1146 | |
| 1147 | For instance, a statistics package may include a container class for |
| 1148 | experimental data. The class provides normal methods for computing the average, |
| 1149 | mean, median, and other descriptive statistics that depend on the data. However, |
| 1150 | there may be useful functions which are conceptually related but do not depend |
| 1151 | on the data. For instance, ``erf(x)`` is handy conversion routine that comes up |
| 1152 | in statistical work but does not directly depend on a particular dataset. |
| 1153 | It can be called either from an object or the class: ``s.erf(1.5) --> .9332`` or |
| 1154 | ``Sample.erf(1.5) --> .9332``. |
| 1155 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 1156 | Since static methods return the underlying function with no changes, the |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1157 | example calls are unexciting: |
| 1158 | |
| 1159 | .. testcode:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1160 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1161 | class E: |
| 1162 | @staticmethod |
| 1163 | def f(x): |
| 1164 | print(x) |
| 1165 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1166 | .. doctest:: |
| 1167 | |
Shubham Aggarwal | abbdd1f | 2019-03-20 08:25:55 +0530 | [diff] [blame] | 1168 | >>> E.f(3) |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1169 | 3 |
Shubham Aggarwal | abbdd1f | 2019-03-20 08:25:55 +0530 | [diff] [blame] | 1170 | >>> E().f(3) |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1171 | 3 |
| 1172 | |
| 1173 | Using the non-data descriptor protocol, a pure Python version of |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1174 | :func:`staticmethod` would look like this: |
| 1175 | |
| 1176 | .. doctest:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1177 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 1178 | class StaticMethod: |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 1179 | "Emulate PyStaticMethod_Type() in Objects/funcobject.c" |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1180 | |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 1181 | def __init__(self, f): |
| 1182 | self.f = f |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1183 | |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 1184 | def __get__(self, obj, objtype=None): |
| 1185 | return self.f |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1186 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 1187 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 1188 | Class methods |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 1189 | ------------- |
| 1190 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1191 | Unlike static methods, class methods prepend the class reference to the |
| 1192 | argument list before calling the function. This format is the same |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1193 | for whether the caller is an object or a class: |
| 1194 | |
| 1195 | .. testcode:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1196 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1197 | class F: |
| 1198 | @classmethod |
| 1199 | def f(cls, x): |
| 1200 | return cls.__name__, x |
| 1201 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1202 | .. doctest:: |
| 1203 | |
| 1204 | >>> F.f(3) |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1205 | ('F', 3) |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1206 | >>> F().f(3) |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1207 | ('F', 3) |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1208 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 1209 | This behavior is useful whenever the method only needs to have a class |
| 1210 | reference and does rely on data stored in a specific instance. One use for |
| 1211 | class methods is to create alternate class constructors. For example, the |
| 1212 | classmethod :func:`dict.fromkeys` creates a new dictionary from a list of |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1213 | keys. The pure Python equivalent is: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1214 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1215 | .. testcode:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1216 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1217 | class Dict(dict): |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1218 | @classmethod |
| 1219 | def fromkeys(cls, iterable, value=None): |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1220 | "Emulate dict_fromkeys() in Objects/dictobject.c" |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1221 | d = cls() |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1222 | for key in iterable: |
| 1223 | d[key] = value |
| 1224 | return d |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1225 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1226 | Now a new dictionary of unique keys can be constructed like this: |
| 1227 | |
| 1228 | .. doctest:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1229 | |
| 1230 | >>> Dict.fromkeys('abracadabra') |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1231 | {'a': None, 'b': None, 'r': None, 'c': None, 'd': None} |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1232 | |
| 1233 | Using the non-data descriptor protocol, a pure Python version of |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1234 | :func:`classmethod` would look like this: |
| 1235 | |
| 1236 | .. testcode:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1237 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 1238 | class ClassMethod: |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 1239 | "Emulate PyClassMethod_Type() in Objects/funcobject.c" |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1240 | |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 1241 | def __init__(self, f): |
| 1242 | self.f = f |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 1243 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 1244 | def __get__(self, obj, cls=None): |
| 1245 | if cls is None: |
| 1246 | cls = type(obj) |
Raymond Hettinger | 8e5b0fd | 2020-10-23 18:37:27 -0700 | [diff] [blame] | 1247 | if hasattr(obj, '__get__'): |
| 1248 | return self.f.__get__(cls) |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 1249 | return MethodType(self.f, cls) |
Raymond Hettinger | 8e5b0fd | 2020-10-23 18:37:27 -0700 | [diff] [blame] | 1250 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1251 | .. testcode:: |
| 1252 | :hide: |
| 1253 | |
| 1254 | # Verify the emulation works |
| 1255 | class T: |
| 1256 | @ClassMethod |
| 1257 | def cm(cls, x, y): |
| 1258 | return (cls, x, y) |
| 1259 | |
| 1260 | .. doctest:: |
| 1261 | :hide: |
| 1262 | |
| 1263 | >>> T.cm(11, 22) |
| 1264 | (<class 'T'>, 11, 22) |
| 1265 | |
| 1266 | # Also call it from an instance |
| 1267 | >>> t = T() |
| 1268 | >>> t.cm(11, 22) |
| 1269 | (<class 'T'>, 11, 22) |
| 1270 | |
Raymond Hettinger | 8e5b0fd | 2020-10-23 18:37:27 -0700 | [diff] [blame] | 1271 | The code path for ``hasattr(obj, '__get__')`` was added in Python 3.9 and |
| 1272 | makes it possible for :func:`classmethod` to support chained decorators. |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1273 | For example, a classmethod and property could be chained together: |
| 1274 | |
| 1275 | .. testcode:: |
Raymond Hettinger | 8e5b0fd | 2020-10-23 18:37:27 -0700 | [diff] [blame] | 1276 | |
| 1277 | class G: |
| 1278 | @classmethod |
| 1279 | @property |
| 1280 | def __doc__(cls): |
| 1281 | return f'A doc for {cls.__name__!r}' |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1282 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1283 | .. doctest:: |
| 1284 | |
| 1285 | >>> G.__doc__ |
| 1286 | "A doc for 'G'" |
| 1287 | |
| 1288 | |
Raymond Hettinger | e9208f0 | 2020-11-01 20:15:50 -0800 | [diff] [blame] | 1289 | Member objects and __slots__ |
| 1290 | ---------------------------- |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1291 | |
| 1292 | When a class defines ``__slots__``, it replaces instance dictionaries with a |
| 1293 | fixed-length array of slot values. From a user point of view that has |
| 1294 | several effects: |
| 1295 | |
| 1296 | 1. Provides immediate detection of bugs due to misspelled attribute |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1297 | assignments. Only attribute names specified in ``__slots__`` are allowed: |
| 1298 | |
| 1299 | .. testcode:: |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1300 | |
| 1301 | class Vehicle: |
| 1302 | __slots__ = ('id_number', 'make', 'model') |
| 1303 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1304 | .. doctest:: |
| 1305 | |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1306 | >>> auto = Vehicle() |
| 1307 | >>> auto.id_nubmer = 'VYE483814LQEX' |
| 1308 | Traceback (most recent call last): |
| 1309 | ... |
| 1310 | AttributeError: 'Vehicle' object has no attribute 'id_nubmer' |
| 1311 | |
| 1312 | 2. Helps create immutable objects where descriptors manage access to private |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1313 | attributes stored in ``__slots__``: |
| 1314 | |
| 1315 | .. testcode:: |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1316 | |
| 1317 | class Immutable: |
| 1318 | |
Raymond Hettinger | 8031877 | 2020-11-06 01:30:17 -0800 | [diff] [blame] | 1319 | __slots__ = ('_dept', '_name') # Replace the instance dictionary |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1320 | |
| 1321 | def __init__(self, dept, name): |
| 1322 | self._dept = dept # Store to private attribute |
| 1323 | self._name = name # Store to private attribute |
| 1324 | |
| 1325 | @property # Read-only descriptor |
| 1326 | def dept(self): |
| 1327 | return self._dept |
| 1328 | |
| 1329 | @property |
| 1330 | def name(self): # Read-only descriptor |
| 1331 | return self._name |
| 1332 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1333 | .. doctest:: |
| 1334 | |
| 1335 | >>> mark = Immutable('Botany', 'Mark Watney') |
| 1336 | >>> mark.dept |
| 1337 | 'Botany' |
| 1338 | >>> mark.dept = 'Space Pirate' |
| 1339 | Traceback (most recent call last): |
| 1340 | ... |
| 1341 | AttributeError: can't set attribute |
| 1342 | >>> mark.location = 'Mars' |
| 1343 | Traceback (most recent call last): |
| 1344 | ... |
| 1345 | AttributeError: 'Immutable' object has no attribute 'location' |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1346 | |
| 1347 | 3. Saves memory. On a 64-bit Linux build, an instance with two attributes |
| 1348 | takes 48 bytes with ``__slots__`` and 152 bytes without. This `flyweight |
| 1349 | design pattern <https://en.wikipedia.org/wiki/Flyweight_pattern>`_ likely only |
| 1350 | matters when a large number of instances are going to be created. |
| 1351 | |
| 1352 | 4. Blocks tools like :func:`functools.cached_property` which require an |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1353 | instance dictionary to function correctly: |
| 1354 | |
| 1355 | .. testcode:: |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1356 | |
| 1357 | from functools import cached_property |
| 1358 | |
| 1359 | class CP: |
| 1360 | __slots__ = () # Eliminates the instance dict |
| 1361 | |
| 1362 | @cached_property # Requires an instance dict |
| 1363 | def pi(self): |
| 1364 | return 4 * sum((-1.0)**n / (2.0*n + 1.0) |
| 1365 | for n in reversed(range(100_000))) |
| 1366 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1367 | .. doctest:: |
| 1368 | |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1369 | >>> CP().pi |
| 1370 | Traceback (most recent call last): |
| 1371 | ... |
| 1372 | TypeError: No '__dict__' attribute on 'CP' instance to cache 'pi' property. |
| 1373 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1374 | It is not possible to create an exact drop-in pure Python version of |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1375 | ``__slots__`` because it requires direct access to C structures and control |
| 1376 | over object memory allocation. However, we can build a mostly faithful |
| 1377 | simulation where the actual C structure for slots is emulated by a private |
| 1378 | ``_slotvalues`` list. Reads and writes to that private structure are managed |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1379 | by member descriptors: |
| 1380 | |
| 1381 | .. testcode:: |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1382 | |
Raymond Hettinger | ffae932 | 2020-11-23 10:56:59 -0800 | [diff] [blame] | 1383 | null = object() |
| 1384 | |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1385 | class Member: |
| 1386 | |
| 1387 | def __init__(self, name, clsname, offset): |
| 1388 | 'Emulate PyMemberDef in Include/structmember.h' |
| 1389 | # Also see descr_new() in Objects/descrobject.c |
| 1390 | self.name = name |
| 1391 | self.clsname = clsname |
| 1392 | self.offset = offset |
| 1393 | |
| 1394 | def __get__(self, obj, objtype=None): |
| 1395 | 'Emulate member_get() in Objects/descrobject.c' |
| 1396 | # Also see PyMember_GetOne() in Python/structmember.c |
Raymond Hettinger | ffae932 | 2020-11-23 10:56:59 -0800 | [diff] [blame] | 1397 | value = obj._slotvalues[self.offset] |
| 1398 | if value is null: |
| 1399 | raise AttributeError(self.name) |
| 1400 | return value |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1401 | |
| 1402 | def __set__(self, obj, value): |
| 1403 | 'Emulate member_set() in Objects/descrobject.c' |
| 1404 | obj._slotvalues[self.offset] = value |
| 1405 | |
Raymond Hettinger | ffae932 | 2020-11-23 10:56:59 -0800 | [diff] [blame] | 1406 | def __delete__(self, obj): |
| 1407 | 'Emulate member_delete() in Objects/descrobject.c' |
| 1408 | value = obj._slotvalues[self.offset] |
| 1409 | if value is null: |
| 1410 | raise AttributeError(self.name) |
| 1411 | obj._slotvalues[self.offset] = null |
| 1412 | |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1413 | def __repr__(self): |
| 1414 | 'Emulate member_repr() in Objects/descrobject.c' |
| 1415 | return f'<Member {self.name!r} of {self.clsname!r}>' |
| 1416 | |
| 1417 | The :meth:`type.__new__` method takes care of adding member objects to class |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1418 | variables: |
| 1419 | |
| 1420 | .. testcode:: |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1421 | |
| 1422 | class Type(type): |
| 1423 | 'Simulate how the type metaclass adds member objects for slots' |
| 1424 | |
| 1425 | def __new__(mcls, clsname, bases, mapping): |
| 1426 | 'Emuluate type_new() in Objects/typeobject.c' |
| 1427 | # type_new() calls PyTypeReady() which calls add_methods() |
| 1428 | slot_names = mapping.get('slot_names', []) |
| 1429 | for offset, name in enumerate(slot_names): |
| 1430 | mapping[name] = Member(name, clsname, offset) |
| 1431 | return type.__new__(mcls, clsname, bases, mapping) |
| 1432 | |
Raymond Hettinger | ffae932 | 2020-11-23 10:56:59 -0800 | [diff] [blame] | 1433 | The :meth:`object.__new__` method takes care of creating instances that have |
| 1434 | slots instead of an instance dictionary. Here is a rough simulation in pure |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1435 | Python: |
| 1436 | |
| 1437 | .. testcode:: |
Raymond Hettinger | ffae932 | 2020-11-23 10:56:59 -0800 | [diff] [blame] | 1438 | |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1439 | class Object: |
| 1440 | 'Simulate how object.__new__() allocates memory for __slots__' |
| 1441 | |
| 1442 | def __new__(cls, *args): |
| 1443 | 'Emulate object_new() in Objects/typeobject.c' |
| 1444 | inst = super().__new__(cls) |
| 1445 | if hasattr(cls, 'slot_names'): |
Raymond Hettinger | ffae932 | 2020-11-23 10:56:59 -0800 | [diff] [blame] | 1446 | empty_slots = [null] * len(cls.slot_names) |
| 1447 | object.__setattr__(inst, '_slotvalues', empty_slots) |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1448 | return inst |
| 1449 | |
Raymond Hettinger | ffae932 | 2020-11-23 10:56:59 -0800 | [diff] [blame] | 1450 | def __setattr__(self, name, value): |
| 1451 | 'Emulate _PyObject_GenericSetAttrWithDict() Objects/object.c' |
| 1452 | cls = type(self) |
| 1453 | if hasattr(cls, 'slot_names') and name not in cls.slot_names: |
| 1454 | raise AttributeError( |
| 1455 | f'{type(self).__name__!r} object has no attribute {name!r}' |
| 1456 | ) |
| 1457 | super().__setattr__(name, value) |
| 1458 | |
| 1459 | def __delattr__(self, name): |
| 1460 | 'Emulate _PyObject_GenericSetAttrWithDict() Objects/object.c' |
| 1461 | cls = type(self) |
| 1462 | if hasattr(cls, 'slot_names') and name not in cls.slot_names: |
| 1463 | raise AttributeError( |
| 1464 | f'{type(self).__name__!r} object has no attribute {name!r}' |
| 1465 | ) |
| 1466 | super().__delattr__(name) |
| 1467 | |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1468 | To use the simulation in a real class, just inherit from :class:`Object` and |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1469 | set the :term:`metaclass` to :class:`Type`: |
| 1470 | |
| 1471 | .. testcode:: |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1472 | |
| 1473 | class H(Object, metaclass=Type): |
Raymond Hettinger | ffae932 | 2020-11-23 10:56:59 -0800 | [diff] [blame] | 1474 | 'Instance variables stored in slots' |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1475 | |
| 1476 | slot_names = ['x', 'y'] |
| 1477 | |
| 1478 | def __init__(self, x, y): |
| 1479 | self.x = x |
| 1480 | self.y = y |
| 1481 | |
| 1482 | At this point, the metaclass has loaded member objects for *x* and *y*:: |
| 1483 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1484 | >>> from pprint import pp |
| 1485 | >>> pp(dict(vars(H))) |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1486 | {'__module__': '__main__', |
Raymond Hettinger | ffae932 | 2020-11-23 10:56:59 -0800 | [diff] [blame] | 1487 | '__doc__': 'Instance variables stored in slots', |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1488 | 'slot_names': ['x', 'y'], |
| 1489 | '__init__': <function H.__init__ at 0x7fb5d302f9d0>, |
| 1490 | 'x': <Member 'x' of 'H'>, |
Raymond Hettinger | ffae932 | 2020-11-23 10:56:59 -0800 | [diff] [blame] | 1491 | 'y': <Member 'y' of 'H'>} |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1492 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1493 | .. doctest:: |
| 1494 | :hide: |
| 1495 | |
| 1496 | # We test this separately because the preceding section is not |
| 1497 | # doctestable due to the hex memory address for the __init__ function |
| 1498 | >>> isinstance(vars(H)['x'], Member) |
| 1499 | True |
| 1500 | >>> isinstance(vars(H)['y'], Member) |
| 1501 | True |
| 1502 | |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1503 | When instances are created, they have a ``slot_values`` list where the |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1504 | attributes are stored: |
| 1505 | |
| 1506 | .. doctest:: |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1507 | |
| 1508 | >>> h = H(10, 20) |
| 1509 | >>> vars(h) |
| 1510 | {'_slotvalues': [10, 20]} |
| 1511 | >>> h.x = 55 |
| 1512 | >>> vars(h) |
| 1513 | {'_slotvalues': [55, 20]} |
| 1514 | |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1515 | Misspelled or unassigned attributes will raise an exception: |
| 1516 | |
| 1517 | .. doctest:: |
Raymond Hettinger | 74fa464 | 2020-11-01 18:02:37 -0800 | [diff] [blame] | 1518 | |
Raymond Hettinger | ffae932 | 2020-11-23 10:56:59 -0800 | [diff] [blame] | 1519 | >>> h.xz |
| 1520 | Traceback (most recent call last): |
| 1521 | ... |
| 1522 | AttributeError: 'H' object has no attribute 'xz' |
Raymond Hettinger | 2d44a6b | 2020-11-24 20:57:02 -0800 | [diff] [blame^] | 1523 | |
| 1524 | .. doctest:: |
| 1525 | :hide: |
| 1526 | |
| 1527 | # Examples for deleted attributes are not shown because this section |
| 1528 | # is already a bit lengthy. We still test that code here. |
| 1529 | >>> del h.x |
| 1530 | >>> hasattr(h, 'x') |
| 1531 | False |
| 1532 | |
| 1533 | # Also test the code for uninitialized slots |
| 1534 | >>> class HU(Object, metaclass=Type): |
| 1535 | ... slot_names = ['x', 'y'] |
| 1536 | ... |
| 1537 | >>> hu = HU() |
| 1538 | >>> hasattr(hu, 'x') |
| 1539 | False |
| 1540 | >>> hasattr(hu, 'y') |
| 1541 | False |