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, |
| 19 | adding one feature at a time. It is a great place to start. |
| 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 |
| 30 | into bound methods or about how to implement common tools like |
| 31 | :func:`classmethod`, :func:`staticmethod`, and :func:`property`. |
| 32 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 33 | |
| 34 | Primer |
| 35 | ^^^^^^ |
| 36 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 37 | In this primer, we start with the most basic possible example and then we'll |
| 38 | add new capabilities one by one. |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | Simple example: A descriptor that returns a constant |
| 42 | ---------------------------------------------------- |
| 43 | |
| 44 | The :class:`Ten` class is a descriptor that always returns the constant ``10``:: |
| 45 | |
| 46 | |
| 47 | class Ten: |
| 48 | def __get__(self, obj, objtype=None): |
| 49 | return 10 |
| 50 | |
| 51 | To use the descriptor, it must be stored as a class variable in another class:: |
| 52 | |
| 53 | class A: |
| 54 | x = 5 # Regular class attribute |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 55 | y = Ten() # Descriptor instance |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 56 | |
| 57 | An interactive session shows the difference between normal attribute lookup |
| 58 | and descriptor lookup:: |
| 59 | |
| 60 | >>> a = A() # Make an instance of class A |
| 61 | >>> a.x # Normal attribute lookup |
| 62 | 5 |
| 63 | >>> a.y # Descriptor lookup |
| 64 | 10 |
| 65 | |
| 66 | In the ``a.x`` attribute lookup, the dot operator finds the value ``5`` stored |
| 67 | in the class dictionary. In the ``a.y`` descriptor lookup, the dot operator |
| 68 | calls the descriptor's :meth:`__get__()` method. That method returns ``10``. |
| 69 | Note that the value ``10`` is not stored in either the class dictionary or the |
| 70 | instance dictionary. Instead, the value ``10`` is computed on demand. |
| 71 | |
| 72 | This example shows how a simple descriptor works, but it isn't very useful. |
| 73 | For retrieving constants, normal attribute lookup would be better. |
| 74 | |
| 75 | In the next section, we'll create something more useful, a dynamic lookup. |
| 76 | |
| 77 | |
| 78 | Dynamic lookups |
| 79 | --------------- |
| 80 | |
| 81 | Interesting descriptors typically run computations instead of doing lookups:: |
| 82 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 83 | import os |
| 84 | |
| 85 | class DirectorySize: |
| 86 | |
| 87 | def __get__(self, obj, objtype=None): |
| 88 | return len(os.listdir(obj.dirname)) |
| 89 | |
| 90 | class Directory: |
| 91 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 92 | size = DirectorySize() # Descriptor instance |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 93 | |
| 94 | def __init__(self, dirname): |
| 95 | self.dirname = dirname # Regular instance attribute |
| 96 | |
| 97 | An interactive session shows that the lookup is dynamic — it computes |
| 98 | different, updated answers each time:: |
| 99 | |
| 100 | >>> g = Directory('games') |
| 101 | >>> s = Directory('songs') |
| 102 | >>> g.size # The games directory has three files |
| 103 | 3 |
| 104 | >>> os.system('touch games/newfile') # Add a fourth file to the directory |
| 105 | 0 |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 106 | >>> g.size # Automatically updated |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 107 | 4 |
| 108 | >>> s.size # The songs directory has twenty files |
| 109 | 20 |
| 110 | |
| 111 | Besides showing how descriptors can run computations, this example also |
| 112 | reveals the purpose of the parameters to :meth:`__get__`. The *self* |
| 113 | parameter is *size*, an instance of *DirectorySize*. The *obj* parameter is |
| 114 | either *g* or *s*, an instance of *Directory*. It is *obj* parameter that |
| 115 | lets the :meth:`__get__` method learn the target directory. The *objtype* |
| 116 | parameter is the class *Directory*. |
| 117 | |
| 118 | |
| 119 | Managed attributes |
| 120 | ------------------ |
| 121 | |
| 122 | A popular use for descriptors is managing access to instance data. The |
| 123 | descriptor is assigned to a public attribute in the class dictionary while the |
| 124 | actual data is stored as a private attribute in the instance dictionary. The |
| 125 | descriptor's :meth:`__get__` and :meth:`__set__` methods are triggered when |
| 126 | the public attribute is accessed. |
| 127 | |
| 128 | In the following example, *age* is the public attribute and *_age* is the |
| 129 | private attribute. When the public attribute is accessed, the descriptor logs |
| 130 | the lookup or update:: |
| 131 | |
| 132 | import logging |
| 133 | |
| 134 | logging.basicConfig(level=logging.INFO) |
| 135 | |
| 136 | class LoggedAgeAccess: |
| 137 | |
| 138 | def __get__(self, obj, objtype=None): |
| 139 | value = obj._age |
| 140 | logging.info('Accessing %r giving %r', 'age', value) |
| 141 | return value |
| 142 | |
| 143 | def __set__(self, obj, value): |
| 144 | logging.info('Updating %r to %r', 'age', value) |
| 145 | obj._age = value |
| 146 | |
| 147 | class Person: |
| 148 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 149 | age = LoggedAgeAccess() # Descriptor instance |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 150 | |
| 151 | def __init__(self, name, age): |
| 152 | self.name = name # Regular instance attribute |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 153 | self.age = age # Calls __set__() |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 154 | |
| 155 | def birthday(self): |
| 156 | self.age += 1 # Calls both __get__() and __set__() |
| 157 | |
| 158 | |
| 159 | An interactive session shows that all access to the managed attribute *age* is |
| 160 | logged, but that the regular attribute *name* is not logged:: |
| 161 | |
| 162 | >>> mary = Person('Mary M', 30) # The initial age update is logged |
| 163 | INFO:root:Updating 'age' to 30 |
| 164 | >>> dave = Person('David D', 40) |
| 165 | INFO:root:Updating 'age' to 40 |
| 166 | |
| 167 | >>> vars(mary) # The actual data is in a private attribute |
| 168 | {'name': 'Mary M', '_age': 30} |
| 169 | >>> vars(dave) |
| 170 | {'name': 'David D', '_age': 40} |
| 171 | |
| 172 | >>> mary.age # Access the data and log the lookup |
| 173 | INFO:root:Accessing 'age' giving 30 |
| 174 | 30 |
| 175 | >>> mary.birthday() # Updates are logged as well |
| 176 | INFO:root:Accessing 'age' giving 30 |
| 177 | INFO:root:Updating 'age' to 31 |
| 178 | |
| 179 | >>> dave.name # Regular attribute lookup isn't logged |
| 180 | 'David D' |
| 181 | >>> dave.age # Only the managed attribute is logged |
| 182 | INFO:root:Accessing 'age' giving 40 |
| 183 | 40 |
| 184 | |
| 185 | One major issue with this example is the private name *_age* is hardwired in |
| 186 | the *LoggedAgeAccess* class. That means that each instance can only have one |
| 187 | logged attribute and that its name is unchangeable. In the next example, |
| 188 | we'll fix that problem. |
| 189 | |
| 190 | |
| 191 | Customized Names |
| 192 | ---------------- |
| 193 | |
| 194 | When a class uses descriptors, it can inform each descriptor about what |
| 195 | variable name was used. |
| 196 | |
| 197 | In this example, the :class:`Person` class has two descriptor instances, |
| 198 | *name* and *age*. When the :class:`Person` class is defined, it makes a |
| 199 | callback to :meth:`__set_name__` in *LoggedAccess* so that the field names can |
| 200 | be recorded, giving each descriptor its own *public_name* and *private_name*:: |
| 201 | |
| 202 | import logging |
| 203 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 204 | logging.basicConfig(level=logging.INFO) |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 205 | |
| 206 | class LoggedAccess: |
| 207 | |
| 208 | def __set_name__(self, owner, name): |
| 209 | self.public_name = name |
| 210 | self.private_name = f'_{name}' |
| 211 | |
| 212 | def __get__(self, obj, objtype=None): |
| 213 | value = getattr(obj, self.private_name) |
| 214 | logging.info('Accessing %r giving %r', self.public_name, value) |
| 215 | return value |
| 216 | |
| 217 | def __set__(self, obj, value): |
| 218 | logging.info('Updating %r to %r', self.public_name, value) |
| 219 | setattr(obj, self.private_name, value) |
| 220 | |
| 221 | class Person: |
| 222 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 223 | name = LoggedAccess() # First descriptor instance |
| 224 | age = LoggedAccess() # Second descriptor instance |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 225 | |
| 226 | def __init__(self, name, age): |
| 227 | self.name = name # Calls the first descriptor |
| 228 | self.age = age # Calls the second descriptor |
| 229 | |
| 230 | def birthday(self): |
| 231 | self.age += 1 |
| 232 | |
| 233 | An interactive session shows that the :class:`Person` class has called |
| 234 | :meth:`__set_name__` so that the field names would be recorded. Here |
| 235 | we call :func:`vars` to lookup the descriptor without triggering it:: |
| 236 | |
| 237 | >>> vars(vars(Person)['name']) |
| 238 | {'public_name': 'name', 'private_name': '_name'} |
| 239 | >>> vars(vars(Person)['age']) |
| 240 | {'public_name': 'age', 'private_name': '_age'} |
| 241 | |
| 242 | The new class now logs access to both *name* and *age*:: |
| 243 | |
| 244 | >>> pete = Person('Peter P', 10) |
| 245 | INFO:root:Updating 'name' to 'Peter P' |
| 246 | INFO:root:Updating 'age' to 10 |
| 247 | >>> kate = Person('Catherine C', 20) |
| 248 | INFO:root:Updating 'name' to 'Catherine C' |
| 249 | INFO:root:Updating 'age' to 20 |
| 250 | |
| 251 | The two *Person* instances contain only the private names:: |
| 252 | |
| 253 | >>> vars(pete) |
| 254 | {'_name': 'Peter P', '_age': 10} |
| 255 | >>> vars(kate) |
| 256 | {'_name': 'Catherine C', '_age': 20} |
| 257 | |
| 258 | |
| 259 | Closing thoughts |
| 260 | ---------------- |
| 261 | |
| 262 | A :term:`descriptor` is what we call any object that defines :meth:`__get__`, |
| 263 | :meth:`__set__`, or :meth:`__delete__`. |
| 264 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 265 | Optionally, descriptors can have a :meth:`__set_name__` method. This is only |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 266 | used in cases where a descriptor needs to know either the class where it was |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 267 | created or the name of class variable it was assigned to. |
| 268 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 269 | Descriptors get invoked by the dot operator during attribute lookup. If a |
| 270 | descriptor is accessed indirectly with ``vars(some_class)[descriptor_name]``, |
| 271 | the descriptor instance is returned without invoking it. |
| 272 | |
| 273 | Descriptors only work when used as class variables. When put in instances, |
| 274 | they have no effect. |
| 275 | |
| 276 | The main motivation for descriptors is to provide a hook allowing objects |
| 277 | stored in class variables to control what happens during dotted lookup. |
| 278 | |
| 279 | Traditionally, the calling class controls what happens during lookup. |
| 280 | Descriptors invert that relationship and allow the data being looked-up to |
| 281 | have a say in the matter. |
| 282 | |
| 283 | Descriptors are used throughout the language. It is how functions turn into |
| 284 | bound methods. Common tools like :func:`classmethod`, :func:`staticmethod`, |
| 285 | :func:`property`, and :func:`functools.cached_property` are all implemented as |
| 286 | descriptors. |
| 287 | |
| 288 | |
| 289 | Complete Practical Example |
| 290 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 291 | |
| 292 | In this example, we create a practical and powerful tool for locating |
| 293 | notoriously hard to find data corruption bugs. |
| 294 | |
| 295 | |
| 296 | Validator class |
| 297 | --------------- |
| 298 | |
| 299 | A validator is a descriptor for managed attribute access. Prior to storing |
| 300 | any data, it verifies that the new value meets various type and range |
| 301 | restrictions. If those restrictions aren't met, it raises an exception to |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 302 | prevent data corruption at its source. |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 303 | |
| 304 | This :class:`Validator` class is both an :term:`abstract base class` and a |
| 305 | managed attribute descriptor:: |
| 306 | |
| 307 | from abc import ABC, abstractmethod |
| 308 | |
| 309 | class Validator(ABC): |
| 310 | |
| 311 | def __set_name__(self, owner, name): |
| 312 | self.private_name = f'_{name}' |
| 313 | |
| 314 | def __get__(self, obj, objtype=None): |
| 315 | return getattr(obj, self.private_name) |
| 316 | |
| 317 | def __set__(self, obj, value): |
| 318 | self.validate(value) |
| 319 | setattr(obj, self.private_name, value) |
| 320 | |
| 321 | @abstractmethod |
| 322 | def validate(self, value): |
| 323 | pass |
| 324 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 325 | Custom validators need to inherit from :class:`Validator` and must supply a |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 326 | :meth:`validate` method to test various restrictions as needed. |
| 327 | |
| 328 | |
| 329 | Custom validators |
| 330 | ----------------- |
| 331 | |
| 332 | Here are three practical data validation utilities: |
| 333 | |
| 334 | 1) :class:`OneOf` verifies that a value is one of a restricted set of options. |
| 335 | |
| 336 | 2) :class:`Number` verifies that a value is either an :class:`int` or |
| 337 | :class:`float`. Optionally, it verifies that a value is between a given |
| 338 | minimum or maximum. |
| 339 | |
| 340 | 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] | 341 | validates a given minimum or maximum length. It can validate a |
| 342 | user-defined `predicate |
| 343 | <https://en.wikipedia.org/wiki/Predicate_(mathematical_logic)>`_ as well. |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 344 | |
| 345 | :: |
| 346 | |
| 347 | class OneOf(Validator): |
| 348 | |
| 349 | def __init__(self, *options): |
| 350 | self.options = set(options) |
| 351 | |
| 352 | def validate(self, value): |
| 353 | if value not in self.options: |
| 354 | raise ValueError(f'Expected {value!r} to be one of {self.options!r}') |
| 355 | |
| 356 | class Number(Validator): |
| 357 | |
| 358 | def __init__(self, minvalue=None, maxvalue=None): |
| 359 | self.minvalue = minvalue |
| 360 | self.maxvalue = maxvalue |
| 361 | |
| 362 | def validate(self, value): |
| 363 | if not isinstance(value, (int, float)): |
| 364 | raise TypeError(f'Expected {value!r} to be an int or float') |
| 365 | if self.minvalue is not None and value < self.minvalue: |
| 366 | raise ValueError( |
| 367 | f'Expected {value!r} to be at least {self.minvalue!r}' |
| 368 | ) |
| 369 | if self.maxvalue is not None and value > self.maxvalue: |
| 370 | raise ValueError( |
| 371 | f'Expected {value!r} to be no more than {self.maxvalue!r}' |
| 372 | ) |
| 373 | |
| 374 | class String(Validator): |
| 375 | |
| 376 | def __init__(self, minsize=None, maxsize=None, predicate=None): |
| 377 | self.minsize = minsize |
| 378 | self.maxsize = maxsize |
| 379 | self.predicate = predicate |
| 380 | |
| 381 | def validate(self, value): |
| 382 | if not isinstance(value, str): |
| 383 | raise TypeError(f'Expected {value!r} to be an str') |
| 384 | if self.minsize is not None and len(value) < self.minsize: |
| 385 | raise ValueError( |
| 386 | f'Expected {value!r} to be no smaller than {self.minsize!r}' |
| 387 | ) |
| 388 | if self.maxsize is not None and len(value) > self.maxsize: |
| 389 | raise ValueError( |
| 390 | f'Expected {value!r} to be no bigger than {self.maxsize!r}' |
| 391 | ) |
| 392 | if self.predicate is not None and not self.predicate(value): |
| 393 | raise ValueError( |
| 394 | f'Expected {self.predicate} to be true for {value!r}' |
| 395 | ) |
| 396 | |
| 397 | |
| 398 | Practical use |
| 399 | ------------- |
| 400 | |
| 401 | Here's how the data validators can be used in a real class:: |
| 402 | |
| 403 | class Component: |
| 404 | |
| 405 | name = String(minsize=3, maxsize=10, predicate=str.isupper) |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 406 | kind = OneOf('wood', 'metal', 'plastic') |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 407 | quantity = Number(minvalue=0) |
| 408 | |
| 409 | def __init__(self, name, kind, quantity): |
| 410 | self.name = name |
| 411 | self.kind = kind |
| 412 | self.quantity = quantity |
| 413 | |
| 414 | The descriptors prevent invalid instances from being created:: |
| 415 | |
| 416 | Component('WIDGET', 'metal', 5) # Allowed. |
| 417 | Component('Widget', 'metal', 5) # Blocked: 'Widget' is not all uppercase |
| 418 | Component('WIDGET', 'metle', 5) # Blocked: 'metle' is misspelled |
| 419 | Component('WIDGET', 'metal', -5) # Blocked: -5 is negative |
| 420 | Component('WIDGET', 'metal', 'V') # Blocked: 'V' isn't a number |
| 421 | |
| 422 | |
| 423 | Technical Tutorial |
| 424 | ^^^^^^^^^^^^^^^^^^ |
| 425 | |
| 426 | What follows is a more technical tutorial for the mechanics and details of how |
| 427 | descriptors work. |
| 428 | |
| 429 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 430 | Abstract |
| 431 | -------- |
| 432 | |
| 433 | Defines descriptors, summarizes the protocol, and shows how descriptors are |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 434 | called. Provides an example showing how object relational mappings work. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 435 | |
| 436 | Learning about descriptors not only provides access to a larger toolset, it |
| 437 | creates a deeper understanding of how Python works and an appreciation for the |
| 438 | elegance of its design. |
| 439 | |
| 440 | |
| 441 | Definition and Introduction |
| 442 | --------------------------- |
| 443 | |
| 444 | In general, a descriptor is an object attribute with "binding behavior", one |
| 445 | whose attribute access has been overridden by methods in the descriptor |
| 446 | protocol. Those methods are :meth:`__get__`, :meth:`__set__`, and |
| 447 | :meth:`__delete__`. If any of those methods are defined for an object, it is |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 448 | said to be a :term:`descriptor`. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 449 | |
| 450 | The default behavior for attribute access is to get, set, or delete the |
| 451 | attribute from an object's dictionary. For instance, ``a.x`` has a lookup chain |
| 452 | starting with ``a.__dict__['x']``, then ``type(a).__dict__['x']``, and |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 453 | continuing through the base classes of ``type(a)``. If the |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 454 | looked-up value is an object defining one of the descriptor methods, then Python |
| 455 | may override the default behavior and invoke the descriptor method instead. |
| 456 | Where this occurs in the precedence chain depends on which descriptor methods |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 457 | were defined. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 458 | |
| 459 | Descriptors are a powerful, general purpose protocol. They are the mechanism |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 460 | behind properties, methods, static methods, class methods, and |
| 461 | :func:`super()`. They are used throughout Python itself. Descriptors |
| 462 | simplify the underlying C code and offer a flexible set of new tools for |
| 463 | everyday Python programs. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 464 | |
| 465 | |
| 466 | Descriptor Protocol |
| 467 | ------------------- |
| 468 | |
NotAFile | 28ea4c2 | 2018-09-10 23:35:38 +0200 | [diff] [blame] | 469 | ``descr.__get__(self, obj, type=None) -> value`` |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 470 | |
NotAFile | 28ea4c2 | 2018-09-10 23:35:38 +0200 | [diff] [blame] | 471 | ``descr.__set__(self, obj, value) -> None`` |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 472 | |
NotAFile | 28ea4c2 | 2018-09-10 23:35:38 +0200 | [diff] [blame] | 473 | ``descr.__delete__(self, obj) -> None`` |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 474 | |
| 475 | That is all there is to it. Define any of these methods and an object is |
| 476 | considered a descriptor and can override default behavior upon being looked up |
| 477 | as an attribute. |
| 478 | |
Aaron Hall, MBA | 4054b17 | 2018-05-20 19:46:42 -0400 | [diff] [blame] | 479 | If an object defines :meth:`__set__` or :meth:`__delete__`, it is considered |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 480 | a data descriptor. Descriptors that only define :meth:`__get__` are called |
| 481 | non-data descriptors (they are typically used for methods but other uses are |
| 482 | possible). |
| 483 | |
| 484 | Data and non-data descriptors differ in how overrides are calculated with |
| 485 | respect to entries in an instance's dictionary. If an instance's dictionary |
| 486 | has an entry with the same name as a data descriptor, the data descriptor |
| 487 | takes precedence. If an instance's dictionary has an entry with the same |
| 488 | name as a non-data descriptor, the dictionary entry takes precedence. |
| 489 | |
| 490 | To make a read-only data descriptor, define both :meth:`__get__` and |
| 491 | :meth:`__set__` with the :meth:`__set__` raising an :exc:`AttributeError` when |
| 492 | called. Defining the :meth:`__set__` method with an exception raising |
| 493 | placeholder is enough to make it a data descriptor. |
| 494 | |
| 495 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 496 | Overview of Descriptor Invocation |
| 497 | --------------------------------- |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 498 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 499 | A descriptor can be called directly with ``desc.__get__(obj)`` or |
| 500 | ``desc.__get__(None, cls)``. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 501 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 502 | 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^] | 503 | attribute access. |
| 504 | |
| 505 | The expression ``obj.x`` looks up the attribute ``x`` in the chain of |
| 506 | namespaces for ``obj``. If the search finds a descriptor, its :meth:`__get__` |
| 507 | method is invoked according to the precedence rules listed below. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 508 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 509 | The details of invocation depend on whether ``obj`` is an object, class, or |
| 510 | instance of super. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 511 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 512 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 513 | Invocation from an Instance |
| 514 | --------------------------- |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 515 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 516 | Instance lookup scans through a chain of namespaces giving data descriptors |
| 517 | the highest priority, followed by instance variables, then non-data |
| 518 | descriptors, then class variables, and lastly :meth:`__getattr__` if it is |
| 519 | provided. |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 520 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 521 | If a descriptor is found for ``a.x``, then it is invoked with: |
| 522 | ``desc.__get__(a, type(a))``. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 523 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 524 | The logic for a dotted lookup is in :meth:`object.__getattribute__`. Here is |
| 525 | a pure Python equivalent:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 526 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 527 | def object_getattribute(obj, name): |
| 528 | "Emulate PyObject_GenericGetAttr() in Objects/object.c" |
| 529 | null = object() |
| 530 | objtype = type(obj) |
| 531 | value = getattr(objtype, name, null) |
| 532 | if value is not null and hasattr(value, '__get__'): |
| 533 | if hasattr(value, '__set__') or hasattr(value, '__delete__'): |
| 534 | return value.__get__(obj, objtype) # data descriptor |
| 535 | try: |
| 536 | return vars(obj)[name] # instance variable |
| 537 | except (KeyError, TypeError): |
| 538 | pass |
| 539 | if hasattr(value, '__get__'): |
| 540 | return value.__get__(obj, objtype) # non-data descriptor |
| 541 | if value is not null: |
| 542 | return value # class variable |
| 543 | # Emulate slot_tp_getattr_hook() in Objects/typeobject.c |
| 544 | if hasattr(objtype, '__getattr__'): |
| 545 | return objtype.__getattr__(obj, name) # __getattr__ hook |
| 546 | raise AttributeError(name) |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 547 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 548 | The :exc:`TypeError` exception handler is needed because the instance dictionary |
| 549 | doesn't exist when its class defines :term:`__slots__`. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 550 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 551 | |
| 552 | Invocation from a Class |
| 553 | ----------------------- |
| 554 | |
| 555 | The logic for a dotted lookup such as ``A.x`` is in |
| 556 | :meth:`type.__getattribute__`. The steps are similar to those for |
| 557 | :meth:`object.__getattribute__` but the instance dictionary lookup is replaced |
| 558 | by a search through the class's :term:`method resolution order`. |
| 559 | |
| 560 | If a descriptor is found, it is invoked with ``desc.__get__(None, A)``. |
| 561 | |
| 562 | The full C implementation can be found in :c:func:`type_getattro()` and |
| 563 | :c:func:`_PyType_Lookup()` in :source:`Objects/typeobject.c`. |
| 564 | |
| 565 | |
| 566 | Invocation from Super |
| 567 | --------------------- |
| 568 | |
| 569 | 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] | 570 | object returned by :class:`super()`. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 571 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 572 | A dotted lookup such as ``super(A, obj).m`` searches ``obj.__class__.__mro__`` |
| 573 | for the base class ``B`` immediately following ``A`` and then returns |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 574 | ``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^] | 575 | unchanged. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 576 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 577 | 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] | 578 | :source:`Objects/typeobject.c`. A pure Python equivalent can be found in |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 579 | `Guido's Tutorial |
| 580 | <https://www.python.org/download/releases/2.2.3/descrintro/#cooperation>`_. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 581 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 582 | |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 583 | Summary of Invocation Logic |
| 584 | --------------------------- |
| 585 | |
| 586 | The mechanism for descriptors is embedded in the :meth:`__getattribute__()` |
| 587 | methods for :class:`object`, :class:`type`, and :func:`super`. |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 588 | |
| 589 | The important points to remember are: |
| 590 | |
| 591 | * Descriptors are invoked by the :meth:`__getattribute__` method. |
| 592 | |
| 593 | * Classes inherit this machinery from :class:`object`, :class:`type`, or |
| 594 | :func:`super`. |
| 595 | |
| 596 | * Overriding :meth:`__getattribute__` prevents automatic descriptor calls |
| 597 | because all the descriptor logic is in that method. |
| 598 | |
| 599 | * :meth:`object.__getattribute__` and :meth:`type.__getattribute__` make |
| 600 | different calls to :meth:`__get__`. The first includes the instance and may |
| 601 | include the class. The second puts in ``None`` for the instance and always |
| 602 | includes the class. |
| 603 | |
| 604 | * Data descriptors always override instance dictionaries. |
| 605 | |
| 606 | * Non-data descriptors may be overridden by instance dictionaries. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 607 | |
| 608 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 609 | Automatic Name Notification |
| 610 | --------------------------- |
| 611 | |
| 612 | Sometimes it is desirable for a descriptor to know what class variable name it |
| 613 | was assigned to. When a new class is created, the :class:`type` metaclass |
| 614 | scans the dictionary of the new class. If any of the entries are descriptors |
| 615 | and if they define :meth:`__set_name__`, that method is called with two |
| 616 | arguments. The *owner* is the class where the descriptor is used, the *name* |
| 617 | is class variable the descriptor was assigned to. |
| 618 | |
| 619 | The implementation details are in :c:func:`type_new()` and |
| 620 | :c:func:`set_names()` in :source:`Objects/typeobject.c`. |
| 621 | |
| 622 | Since the update logic is in :meth:`type.__new__`, notifications only take |
| 623 | place at the time of class creation. If descriptors are added to the class |
| 624 | afterwards, :meth:`__set_name__` will need to be called manually. |
| 625 | |
| 626 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 627 | ORM Example |
| 628 | ----------- |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 629 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 630 | The following code is simplified skeleton showing how data descriptors could |
| 631 | be used to implement an `object relational mapping |
| 632 | <https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping>`_. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 633 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 634 | The essential idea is that the data is stored in an external database. The |
| 635 | Python instances only hold keys to the database's tables. Descriptors take |
| 636 | care of lookups or updates:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 637 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 638 | class Field: |
| 639 | |
| 640 | def __set_name__(self, owner, name): |
| 641 | self.fetch = f'SELECT {name} FROM {owner.table} WHERE {owner.key}=?;' |
| 642 | self.store = f'UPDATE {owner.table} SET {name}=? WHERE {owner.key}=?;' |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 643 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 644 | def __get__(self, obj, objtype=None): |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 645 | return conn.execute(self.fetch, [obj.key]).fetchone()[0] |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 646 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 647 | def __set__(self, obj, value): |
| 648 | conn.execute(self.store, [value, obj.key]) |
| 649 | conn.commit() |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 650 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 651 | We can use the :class:`Field` class to define "models" that describe the schema |
| 652 | for each table in a database:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 653 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 654 | class Movie: |
| 655 | table = 'Movies' # Table name |
| 656 | key = 'title' # Primary key |
| 657 | director = Field() |
| 658 | year = Field() |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 659 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 660 | def __init__(self, key): |
| 661 | self.key = key |
| 662 | |
| 663 | class Song: |
| 664 | table = 'Music' |
| 665 | key = 'title' |
| 666 | artist = Field() |
| 667 | year = Field() |
| 668 | genre = Field() |
| 669 | |
| 670 | def __init__(self, key): |
| 671 | self.key = key |
| 672 | |
| 673 | An interactive session shows how data is retrieved from the database and how |
| 674 | it can be updated:: |
| 675 | |
| 676 | >>> import sqlite3 |
| 677 | >>> conn = sqlite3.connect('entertainment.db') |
| 678 | |
| 679 | >>> Movie('Star Wars').director |
| 680 | 'George Lucas' |
| 681 | >>> jaws = Movie('Jaws') |
| 682 | >>> f'Released in {jaws.year} by {jaws.director}' |
| 683 | 'Released in 1975 by Steven Spielberg' |
| 684 | |
| 685 | >>> Song('Country Roads').artist |
| 686 | 'John Denver' |
| 687 | |
| 688 | >>> Movie('Star Wars').director = 'J.J. Abrams' |
| 689 | >>> Movie('Star Wars').director |
| 690 | 'J.J. Abrams' |
| 691 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 692 | Pure Python Equivalents |
| 693 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 694 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 695 | The descriptor protocol is simple and offers exciting possibilities. Several |
Raymond Hettinger | 148c76b | 2020-11-01 09:10:06 -0800 | [diff] [blame^] | 696 | use cases are so common that they have been prepackaged into built-in tools. |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 697 | Properties, bound methods, static methods, and class methods are all based on |
| 698 | the descriptor protocol. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 699 | |
| 700 | |
| 701 | Properties |
| 702 | ---------- |
| 703 | |
| 704 | Calling :func:`property` is a succinct way of building a data descriptor that |
| 705 | triggers function calls upon access to an attribute. Its signature is:: |
| 706 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 707 | property(fget=None, fset=None, fdel=None, doc=None) -> property |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 708 | |
| 709 | The documentation shows a typical use to define a managed attribute ``x``:: |
| 710 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 711 | class C: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 712 | def getx(self): return self.__x |
| 713 | def setx(self, value): self.__x = value |
| 714 | def delx(self): del self.__x |
| 715 | x = property(getx, setx, delx, "I'm the 'x' property.") |
| 716 | |
| 717 | To see how :func:`property` is implemented in terms of the descriptor protocol, |
| 718 | here is a pure Python equivalent:: |
| 719 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 720 | class Property: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 721 | "Emulate PyProperty_Type() in Objects/descrobject.c" |
| 722 | |
| 723 | def __init__(self, fget=None, fset=None, fdel=None, doc=None): |
| 724 | self.fget = fget |
| 725 | self.fset = fset |
| 726 | self.fdel = fdel |
Raymond Hettinger | 632c8c8 | 2013-03-10 09:41:18 -0700 | [diff] [blame] | 727 | if doc is None and fget is not None: |
| 728 | doc = fget.__doc__ |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 729 | self.__doc__ = doc |
| 730 | |
| 731 | def __get__(self, obj, objtype=None): |
| 732 | if obj is None: |
| 733 | return self |
| 734 | if self.fget is None: |
Raymond Hettinger | 632c8c8 | 2013-03-10 09:41:18 -0700 | [diff] [blame] | 735 | raise AttributeError("unreadable attribute") |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 736 | return self.fget(obj) |
| 737 | |
| 738 | def __set__(self, obj, value): |
| 739 | if self.fset is None: |
Raymond Hettinger | 632c8c8 | 2013-03-10 09:41:18 -0700 | [diff] [blame] | 740 | raise AttributeError("can't set attribute") |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 741 | self.fset(obj, value) |
| 742 | |
| 743 | def __delete__(self, obj): |
| 744 | if self.fdel is None: |
Raymond Hettinger | 632c8c8 | 2013-03-10 09:41:18 -0700 | [diff] [blame] | 745 | raise AttributeError("can't delete attribute") |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 746 | self.fdel(obj) |
| 747 | |
Raymond Hettinger | 632c8c8 | 2013-03-10 09:41:18 -0700 | [diff] [blame] | 748 | def getter(self, fget): |
| 749 | return type(self)(fget, self.fset, self.fdel, self.__doc__) |
| 750 | |
| 751 | def setter(self, fset): |
| 752 | return type(self)(self.fget, fset, self.fdel, self.__doc__) |
| 753 | |
| 754 | def deleter(self, fdel): |
| 755 | return type(self)(self.fget, self.fset, fdel, self.__doc__) |
| 756 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 757 | The :func:`property` builtin helps whenever a user interface has granted |
| 758 | attribute access and then subsequent changes require the intervention of a |
| 759 | method. |
| 760 | |
| 761 | For instance, a spreadsheet class may grant access to a cell value through |
| 762 | ``Cell('b10').value``. Subsequent improvements to the program require the cell |
| 763 | to be recalculated on every access; however, the programmer does not want to |
| 764 | affect existing client code accessing the attribute directly. The solution is |
| 765 | to wrap access to the value attribute in a property data descriptor:: |
| 766 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 767 | class Cell: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 768 | ... |
| 769 | |
| 770 | @property |
| 771 | def value(self): |
_ = NaN | b066edf | 2017-06-23 11:54:35 +0800 | [diff] [blame] | 772 | "Recalculate the cell before returning value" |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 773 | self.recalc() |
_ = NaN | b066edf | 2017-06-23 11:54:35 +0800 | [diff] [blame] | 774 | return self._value |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 775 | |
| 776 | |
| 777 | Functions and Methods |
| 778 | --------------------- |
| 779 | |
| 780 | Python's object oriented features are built upon a function based environment. |
| 781 | Using non-data descriptors, the two are merged seamlessly. |
| 782 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 783 | Functions stored in class dictionaries get turned into methods when invoked. |
| 784 | Methods only differ from regular functions in that the object instance is |
| 785 | prepended to the other arguments. By convention, the instance is called |
| 786 | *self* but could be called *this* or any other variable name. |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 787 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 788 | Methods can be created manually with :class:`types.MethodType` which is |
| 789 | roughly equivalent to:: |
| 790 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 791 | class MethodType: |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 792 | "Emulate Py_MethodType in Objects/classobject.c" |
| 793 | |
| 794 | def __init__(self, func, obj): |
| 795 | self.__func__ = func |
| 796 | self.__self__ = obj |
| 797 | |
| 798 | def __call__(self, *args, **kwargs): |
| 799 | func = self.__func__ |
| 800 | obj = self.__self__ |
| 801 | return func(obj, *args, **kwargs) |
| 802 | |
| 803 | To support automatic creation of methods, functions include the |
| 804 | :meth:`__get__` method for binding methods during attribute access. This |
| 805 | means that functions are non-data descriptors which return bound methods |
| 806 | during dotted lookup from an instance. Here's how it works:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 807 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 808 | class Function: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 809 | ... |
| 810 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 811 | def __get__(self, obj, objtype=None): |
| 812 | "Simulate func_descr_get() in Objects/funcobject.c" |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 813 | if obj is None: |
| 814 | return self |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 815 | return MethodType(self, obj) |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 816 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 817 | Running the following class in the interpreter shows how the function |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 818 | descriptor works in practice:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 819 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 820 | class D: |
| 821 | def f(self, x): |
| 822 | return x |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 823 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 824 | The function has a :term:`qualified name` attribute to support introspection:: |
| 825 | |
| 826 | >>> D.f.__qualname__ |
| 827 | 'D.f' |
| 828 | |
| 829 | Accessing the function through the class dictionary does not invoke |
| 830 | :meth:`__get__`. Instead, it just returns the underlying function object:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 831 | |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 832 | >>> D.__dict__['f'] |
| 833 | <function D.f at 0x00C45070> |
| 834 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 835 | Dotted access from a class calls :meth:`__get__` which just returns the |
| 836 | underlying function unchanged:: |
| 837 | |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 838 | >>> D.f |
| 839 | <function D.f at 0x00C45070> |
| 840 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 841 | The interesting behavior occurs during dotted access from an instance. The |
| 842 | dotted lookup calls :meth:`__get__` which returns a bound method object:: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 843 | |
| 844 | >>> d = D() |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 845 | >>> d.f |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 846 | <bound method D.f of <__main__.D object at 0x00B18C90>> |
| 847 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 848 | Internally, the bound method stores the underlying function and the bound |
| 849 | instance:: |
| 850 | |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 851 | >>> d.f.__func__ |
| 852 | <function D.f at 0x1012e5ae8> |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 853 | |
Raymond Hettinger | 0d4497b | 2017-09-25 01:05:49 -0700 | [diff] [blame] | 854 | >>> d.f.__self__ |
| 855 | <__main__.D object at 0x1012e1f98> |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 856 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 857 | If you have ever wondered where *self* comes from in regular methods or where |
| 858 | *cls* comes from in class methods, this is it! |
| 859 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 860 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 861 | Static Methods |
| 862 | -------------- |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 863 | |
| 864 | Non-data descriptors provide a simple mechanism for variations on the usual |
| 865 | patterns of binding functions into methods. |
| 866 | |
| 867 | 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] | 868 | 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] | 869 | ``obj.f(*args)`` call into ``f(obj, *args)``. Calling ``cls.f(*args)`` |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 870 | becomes ``f(*args)``. |
| 871 | |
| 872 | This chart summarizes the binding and its two most useful variants: |
| 873 | |
| 874 | +-----------------+----------------------+------------------+ |
| 875 | | Transformation | Called from an | Called from a | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 876 | | | object | class | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 877 | +=================+======================+==================+ |
| 878 | | function | f(obj, \*args) | f(\*args) | |
| 879 | +-----------------+----------------------+------------------+ |
| 880 | | staticmethod | f(\*args) | f(\*args) | |
| 881 | +-----------------+----------------------+------------------+ |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 882 | | classmethod | f(type(obj), \*args) | f(cls, \*args) | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 883 | +-----------------+----------------------+------------------+ |
| 884 | |
| 885 | Static methods return the underlying function without changes. Calling either |
| 886 | ``c.f`` or ``C.f`` is the equivalent of a direct lookup into |
| 887 | ``object.__getattribute__(c, "f")`` or ``object.__getattribute__(C, "f")``. As a |
| 888 | result, the function becomes identically accessible from either an object or a |
| 889 | class. |
| 890 | |
| 891 | Good candidates for static methods are methods that do not reference the |
| 892 | ``self`` variable. |
| 893 | |
| 894 | For instance, a statistics package may include a container class for |
| 895 | experimental data. The class provides normal methods for computing the average, |
| 896 | mean, median, and other descriptive statistics that depend on the data. However, |
| 897 | there may be useful functions which are conceptually related but do not depend |
| 898 | on the data. For instance, ``erf(x)`` is handy conversion routine that comes up |
| 899 | in statistical work but does not directly depend on a particular dataset. |
| 900 | It can be called either from an object or the class: ``s.erf(1.5) --> .9332`` or |
| 901 | ``Sample.erf(1.5) --> .9332``. |
| 902 | |
Raymond Hettinger | 4a9c637 | 2020-10-24 20:34:39 -0700 | [diff] [blame] | 903 | Since static methods return the underlying function with no changes, the |
| 904 | example calls are unexciting:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 905 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 906 | class E: |
| 907 | @staticmethod |
| 908 | def f(x): |
| 909 | print(x) |
| 910 | |
Shubham Aggarwal | abbdd1f | 2019-03-20 08:25:55 +0530 | [diff] [blame] | 911 | >>> E.f(3) |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 912 | 3 |
Shubham Aggarwal | abbdd1f | 2019-03-20 08:25:55 +0530 | [diff] [blame] | 913 | >>> E().f(3) |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 914 | 3 |
| 915 | |
| 916 | Using the non-data descriptor protocol, a pure Python version of |
| 917 | :func:`staticmethod` would look like this:: |
| 918 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 919 | class StaticMethod: |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 920 | "Emulate PyStaticMethod_Type() in Objects/funcobject.c" |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 921 | |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 922 | def __init__(self, f): |
| 923 | self.f = f |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 924 | |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 925 | def __get__(self, obj, objtype=None): |
| 926 | return self.f |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 927 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 928 | |
| 929 | Class Methods |
| 930 | ------------- |
| 931 | |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 932 | Unlike static methods, class methods prepend the class reference to the |
| 933 | argument list before calling the function. This format is the same |
| 934 | for whether the caller is an object or a class:: |
| 935 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 936 | class F: |
| 937 | @classmethod |
| 938 | def f(cls, x): |
| 939 | return cls.__name__, x |
| 940 | |
| 941 | >>> print(F.f(3)) |
| 942 | ('F', 3) |
| 943 | >>> print(F().f(3)) |
| 944 | ('F', 3) |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 945 | |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 946 | This behavior is useful whenever the method only needs to have a class |
| 947 | reference and does rely on data stored in a specific instance. One use for |
| 948 | class methods is to create alternate class constructors. For example, the |
| 949 | classmethod :func:`dict.fromkeys` creates a new dictionary from a list of |
| 950 | keys. The pure Python equivalent is:: |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 951 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 952 | class Dict: |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 953 | ... |
| 954 | |
| 955 | @classmethod |
| 956 | def fromkeys(cls, iterable, value=None): |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 957 | "Emulate dict_fromkeys() in Objects/dictobject.c" |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 958 | d = cls() |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 959 | for key in iterable: |
| 960 | d[key] = value |
| 961 | return d |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 962 | |
| 963 | Now a new dictionary of unique keys can be constructed like this:: |
| 964 | |
| 965 | >>> Dict.fromkeys('abracadabra') |
| 966 | {'a': None, 'r': None, 'b': None, 'c': None, 'd': None} |
| 967 | |
| 968 | Using the non-data descriptor protocol, a pure Python version of |
| 969 | :func:`classmethod` would look like this:: |
| 970 | |
Serhiy Storchaka | e042a45 | 2019-06-10 13:35:52 +0300 | [diff] [blame] | 971 | class ClassMethod: |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 972 | "Emulate PyClassMethod_Type() in Objects/funcobject.c" |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 973 | |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 974 | def __init__(self, f): |
| 975 | self.f = f |
Georg Brandl | 45cceeb | 2010-05-19 21:39:51 +0000 | [diff] [blame] | 976 | |
Raymond Hettinger | 8d3d731 | 2020-10-23 12:55:39 -0700 | [diff] [blame] | 977 | def __get__(self, obj, cls=None): |
| 978 | if cls is None: |
| 979 | cls = type(obj) |
Raymond Hettinger | 8e5b0fd | 2020-10-23 18:37:27 -0700 | [diff] [blame] | 980 | if hasattr(obj, '__get__'): |
| 981 | return self.f.__get__(cls) |
Raymond Hettinger | e6a7ea4 | 2020-10-25 07:12:50 -0700 | [diff] [blame] | 982 | return MethodType(self.f, cls) |
Raymond Hettinger | 8e5b0fd | 2020-10-23 18:37:27 -0700 | [diff] [blame] | 983 | |
| 984 | The code path for ``hasattr(obj, '__get__')`` was added in Python 3.9 and |
| 985 | makes it possible for :func:`classmethod` to support chained decorators. |
| 986 | For example, a classmethod and property could be chained together:: |
| 987 | |
| 988 | class G: |
| 989 | @classmethod |
| 990 | @property |
| 991 | def __doc__(cls): |
| 992 | return f'A doc for {cls.__name__!r}' |