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