blob: bed4078e3a3a9db01ace7e6050e685c4296302cd [file] [log] [blame]
Raymond Hettinger8d3d7312020-10-23 12:55:39 -07001.. _descriptorhowto:
2
Georg Brandl45cceeb2010-05-19 21:39:51 +00003======================
4Descriptor HowTo Guide
5======================
6
7:Author: Raymond Hettinger
8:Contact: <python at rcn dot com>
9
10.. Contents::
11
Raymond Hettinger8d3d7312020-10-23 12:55:39 -070012
13:term:`Descriptors <descriptor>` let objects customize attribute lookup,
14storage, and deletion.
15
16This HowTo guide has three major sections:
17
181) 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
212) The second section shows a complete, practical descriptor example. If you
22 already know the basics, start there.
23
243) 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
29Primer
30^^^^^^
31
Raymond Hettinger4a9c6372020-10-24 20:34:39 -070032In this primer, we start with the most basic possible example and then we'll
33add new capabilities one by one.
Raymond Hettinger8d3d7312020-10-23 12:55:39 -070034
35
36Simple example: A descriptor that returns a constant
37----------------------------------------------------
38
39The :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
46To 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
52An interactive session shows the difference between normal attribute lookup
53and 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
61In the ``a.x`` attribute lookup, the dot operator finds the value ``5`` stored
62in the class dictionary. In the ``a.y`` descriptor lookup, the dot operator
63calls the descriptor's :meth:`__get__()` method. That method returns ``10``.
64Note that the value ``10`` is not stored in either the class dictionary or the
65instance dictionary. Instead, the value ``10`` is computed on demand.
66
67This example shows how a simple descriptor works, but it isn't very useful.
68For retrieving constants, normal attribute lookup would be better.
69
70In the next section, we'll create something more useful, a dynamic lookup.
71
72
73Dynamic lookups
74---------------
75
76Interesting 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
93An interactive session shows that the lookup is dynamic — it computes
94different, 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
107Besides showing how descriptors can run computations, this example also
108reveals the purpose of the parameters to :meth:`__get__`. The *self*
109parameter is *size*, an instance of *DirectorySize*. The *obj* parameter is
110either *g* or *s*, an instance of *Directory*. It is *obj* parameter that
111lets the :meth:`__get__` method learn the target directory. The *objtype*
112parameter is the class *Directory*.
113
114
115Managed attributes
116------------------
117
118A popular use for descriptors is managing access to instance data. The
119descriptor is assigned to a public attribute in the class dictionary while the
120actual data is stored as a private attribute in the instance dictionary. The
121descriptor's :meth:`__get__` and :meth:`__set__` methods are triggered when
122the public attribute is accessed.
123
124In the following example, *age* is the public attribute and *_age* is the
125private attribute. When the public attribute is accessed, the descriptor logs
126the 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
155An interactive session shows that all access to the managed attribute *age* is
156logged, 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
181One major issue with this example is the private name *_age* is hardwired in
182the *LoggedAgeAccess* class. That means that each instance can only have one
183logged attribute and that its name is unchangeable. In the next example,
184we'll fix that problem.
185
186
187Customized Names
188----------------
189
190When a class uses descriptors, it can inform each descriptor about what
191variable name was used.
192
193In this example, the :class:`Person` class has two descriptor instances,
194*name* and *age*. When the :class:`Person` class is defined, it makes a
195callback to :meth:`__set_name__` in *LoggedAccess* so that the field names can
196be recorded, giving each descriptor its own *public_name* and *private_name*::
197
198 import logging
199
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700200 logging.basicConfig(level=logging.INFO, force=True)
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700201
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
229An interactive session shows that the :class:`Person` class has called
230:meth:`__set_name__` so that the field names would be recorded. Here
231we 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
238The 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
247The 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
255Closing thoughts
256----------------
257
258A :term:`descriptor` is what we call any object that defines :meth:`__get__`,
259:meth:`__set__`, or :meth:`__delete__`.
260
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700261Optionally, descriptors can have a :meth:`__set_name__` method. This is only
262used in cases where a descriptor needs to know either the class where it is
263created or the name of class variable it was assigned to.
264
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700265Descriptors get invoked by the dot operator during attribute lookup. If a
266descriptor is accessed indirectly with ``vars(some_class)[descriptor_name]``,
267the descriptor instance is returned without invoking it.
268
269Descriptors only work when used as class variables. When put in instances,
270they have no effect.
271
272The main motivation for descriptors is to provide a hook allowing objects
273stored in class variables to control what happens during dotted lookup.
274
275Traditionally, the calling class controls what happens during lookup.
276Descriptors invert that relationship and allow the data being looked-up to
277have a say in the matter.
278
279Descriptors are used throughout the language. It is how functions turn into
280bound methods. Common tools like :func:`classmethod`, :func:`staticmethod`,
281:func:`property`, and :func:`functools.cached_property` are all implemented as
282descriptors.
283
284
285Complete Practical Example
286^^^^^^^^^^^^^^^^^^^^^^^^^^
287
288In this example, we create a practical and powerful tool for locating
289notoriously hard to find data corruption bugs.
290
291
292Validator class
293---------------
294
295A validator is a descriptor for managed attribute access. Prior to storing
296any data, it verifies that the new value meets various type and range
297restrictions. If those restrictions aren't met, it raises an exception to
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700298prevent data corruption at its source.
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700299
300This :class:`Validator` class is both an :term:`abstract base class` and a
301managed 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
321Custom validators need to subclass from :class:`Validator` and supply a
322:meth:`validate` method to test various restrictions as needed.
323
324
325Custom validators
326-----------------
327
328Here are three practical data validation utilities:
329
3301) :class:`OneOf` verifies that a value is one of a restricted set of options.
331
3322) :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
3363) :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
393Practical use
394-------------
395
396Here'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
409The 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
418Technical Tutorial
419^^^^^^^^^^^^^^^^^^
420
421What follows is a more technical tutorial for the mechanics and details of how
422descriptors work.
423
424
Georg Brandl45cceeb2010-05-19 21:39:51 +0000425Abstract
426--------
427
428Defines descriptors, summarizes the protocol, and shows how descriptors are
Andrés Delfino271818f2018-09-14 14:13:09 -0300429called. Examines a custom descriptor and several built-in Python descriptors
Georg Brandl45cceeb2010-05-19 21:39:51 +0000430including functions, properties, static methods, and class methods. Shows how
431each works by giving a pure Python equivalent and a sample application.
432
433Learning about descriptors not only provides access to a larger toolset, it
434creates a deeper understanding of how Python works and an appreciation for the
435elegance of its design.
436
437
438Definition and Introduction
439---------------------------
440
441In general, a descriptor is an object attribute with "binding behavior", one
442whose attribute access has been overridden by methods in the descriptor
443protocol. Those methods are :meth:`__get__`, :meth:`__set__`, and
444:meth:`__delete__`. If any of those methods are defined for an object, it is
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700445said to be a :term:`descriptor`.
Georg Brandl45cceeb2010-05-19 21:39:51 +0000446
447The default behavior for attribute access is to get, set, or delete the
448attribute from an object's dictionary. For instance, ``a.x`` has a lookup chain
449starting with ``a.__dict__['x']``, then ``type(a).__dict__['x']``, and
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700450continuing through the base classes of ``type(a)``. If the
Georg Brandl45cceeb2010-05-19 21:39:51 +0000451looked-up value is an object defining one of the descriptor methods, then Python
452may override the default behavior and invoke the descriptor method instead.
453Where this occurs in the precedence chain depends on which descriptor methods
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100454were defined.
Georg Brandl45cceeb2010-05-19 21:39:51 +0000455
456Descriptors are a powerful, general purpose protocol. They are the mechanism
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700457behind properties, methods, static methods, class methods, and
458:func:`super()`. They are used throughout Python itself. Descriptors
459simplify the underlying C code and offer a flexible set of new tools for
460everyday Python programs.
Georg Brandl45cceeb2010-05-19 21:39:51 +0000461
462
463Descriptor Protocol
464-------------------
465
NotAFile28ea4c22018-09-10 23:35:38 +0200466``descr.__get__(self, obj, type=None) -> value``
Georg Brandl45cceeb2010-05-19 21:39:51 +0000467
NotAFile28ea4c22018-09-10 23:35:38 +0200468``descr.__set__(self, obj, value) -> None``
Georg Brandl45cceeb2010-05-19 21:39:51 +0000469
NotAFile28ea4c22018-09-10 23:35:38 +0200470``descr.__delete__(self, obj) -> None``
Georg Brandl45cceeb2010-05-19 21:39:51 +0000471
472That is all there is to it. Define any of these methods and an object is
473considered a descriptor and can override default behavior upon being looked up
474as an attribute.
475
Aaron Hall, MBA4054b172018-05-20 19:46:42 -0400476If an object defines :meth:`__set__` or :meth:`__delete__`, it is considered
Georg Brandl45cceeb2010-05-19 21:39:51 +0000477a data descriptor. Descriptors that only define :meth:`__get__` are called
478non-data descriptors (they are typically used for methods but other uses are
479possible).
480
481Data and non-data descriptors differ in how overrides are calculated with
482respect to entries in an instance's dictionary. If an instance's dictionary
483has an entry with the same name as a data descriptor, the data descriptor
484takes precedence. If an instance's dictionary has an entry with the same
485name as a non-data descriptor, the dictionary entry takes precedence.
486
487To make a read-only data descriptor, define both :meth:`__get__` and
488:meth:`__set__` with the :meth:`__set__` raising an :exc:`AttributeError` when
489called. Defining the :meth:`__set__` method with an exception raising
490placeholder is enough to make it a data descriptor.
491
492
493Invoking Descriptors
494--------------------
495
496A descriptor can be called directly by its method name. For example,
497``d.__get__(obj)``.
498
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700499But it is more common for a descriptor to be invoked automatically from
500attribute 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 Brandl45cceeb2010-05-19 21:39:51 +0000502is invoked according to the precedence rules listed below.
503
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700504The details of invocation depend on whether ``obj`` is an object, class, or
505instance of super.
Georg Brandl45cceeb2010-05-19 21:39:51 +0000506
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700507**Objects**: The machinery is in :meth:`object.__getattribute__`.
508
509It transforms ``b.x`` into ``type(b).__dict__['x'].__get__(b, type(b))``.
510
511The implementation works through a precedence chain that gives data descriptors
Georg Brandl45cceeb2010-05-19 21:39:51 +0000512priority over instance variables, instance variables priority over non-data
Benjamin Peterson57fb11b2014-10-06 21:10:25 -0400513descriptors, and assigns lowest priority to :meth:`__getattr__` if provided.
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700514
Benjamin Peterson57fb11b2014-10-06 21:10:25 -0400515The full C implementation can be found in :c:func:`PyObject_GenericGetAttr()` in
516:source:`Objects/object.c`.
Georg Brandl45cceeb2010-05-19 21:39:51 +0000517
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700518**Classes**: The machinery is in :meth:`type.__getattribute__`.
Georg Brandl45cceeb2010-05-19 21:39:51 +0000519
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700520It transforms ``A.x`` into ``A.__dict__['x'].__get__(None, A)``.
521
522In pure Python, it looks like this::
523
524 def __getattribute__(cls, key):
Georg Brandl45cceeb2010-05-19 21:39:51 +0000525 "Emulate type_getattro() in Objects/typeobject.c"
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700526 v = object.__getattribute__(cls, key)
Georg Brandl45cceeb2010-05-19 21:39:51 +0000527 if hasattr(v, '__get__'):
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700528 return v.__get__(None, cls)
Georg Brandl45cceeb2010-05-19 21:39:51 +0000529 return v
530
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700531**Super**: The machinery is in the custom :meth:`__getattribute__` method for
532object returned by :class:`super()`.
Georg Brandl45cceeb2010-05-19 21:39:51 +0000533
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700534The attribute lookup ``super(A, obj).m`` searches ``obj.__class__.__mro__`` for
535the base class ``B`` immediately following ``A`` and then returns
536``B.__dict__['m'].__get__(obj, A)``.
Georg Brandl45cceeb2010-05-19 21:39:51 +0000537
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700538If not a descriptor, ``m`` is returned unchanged. If not in the dictionary,
539``m`` reverts to a search using :meth:`object.__getattribute__`.
Georg Brandl45cceeb2010-05-19 21:39:51 +0000540
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100541The implementation details are in :c:func:`super_getattro()` in
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700542:source:`Objects/typeobject.c`. A pure Python equivalent can be found in
Benjamin Peterson57fb11b2014-10-06 21:10:25 -0400543`Guido's Tutorial`_.
Georg Brandl45cceeb2010-05-19 21:39:51 +0000544
Georg Brandl9bdcb3b2014-10-29 09:37:43 +0100545.. _`Guido's Tutorial`: https://www.python.org/download/releases/2.2.3/descrintro/#cooperation
Georg Brandl45cceeb2010-05-19 21:39:51 +0000546
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700547**Summary**: The details listed above show that the mechanism for descriptors is
548embedded in the :meth:`__getattribute__()` methods for :class:`object`,
549:class:`type`, and :func:`super`.
550
551The 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 Brandl45cceeb2010-05-19 21:39:51 +0000569
570
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700571Automatic Name Notification
572---------------------------
573
574Sometimes it is desirable for a descriptor to know what class variable name it
575was assigned to. When a new class is created, the :class:`type` metaclass
576scans the dictionary of the new class. If any of the entries are descriptors
577and if they define :meth:`__set_name__`, that method is called with two
578arguments. The *owner* is the class where the descriptor is used, the *name*
579is class variable the descriptor was assigned to.
580
581The implementation details are in :c:func:`type_new()` and
582:c:func:`set_names()` in :source:`Objects/typeobject.c`.
583
584Since the update logic is in :meth:`type.__new__`, notifications only take
585place at the time of class creation. If descriptors are added to the class
586afterwards, :meth:`__set_name__` will need to be called manually.
587
588
Georg Brandl45cceeb2010-05-19 21:39:51 +0000589Descriptor Example
590------------------
591
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700592The following code is simplified skeleton showing how data descriptors could
593be used to implement an `object relational mapping
594<https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping>`_.
Georg Brandl45cceeb2010-05-19 21:39:51 +0000595
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700596The essential idea is that instances only hold keys to a database table. The
597actual data is stored in an external table that is being dynamically updated::
Georg Brandl45cceeb2010-05-19 21:39:51 +0000598
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700599 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 Brandl45cceeb2010-05-19 21:39:51 +0000604
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700605 def __get__(self, obj, objtype=None):
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700606 return conn.execute(self.fetch, [obj.key]).fetchone()[0]
Georg Brandl45cceeb2010-05-19 21:39:51 +0000607
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700608 def __set__(self, obj, value):
609 conn.execute(self.store, [value, obj.key])
610 conn.commit()
Georg Brandl45cceeb2010-05-19 21:39:51 +0000611
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700612We can use the :class:`Field` to define "models" that describe the schema for
613each table in a database::
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700614
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700615 class Movie:
616 table = 'Movies' # Table name
617 key = 'title' # Primary key
618 director = Field()
619 year = Field()
Georg Brandl45cceeb2010-05-19 21:39:51 +0000620
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700621 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
634An interactive session shows how data is retrieved from the database and how
635it 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
653The descriptor protocol is simple and offers exciting possibilities. Several
654use cases are so common that they have been packaged into individual function
655calls. Properties, bound methods, static methods, and class methods are all
Georg Brandl45cceeb2010-05-19 21:39:51 +0000656based on the descriptor protocol.
657
658
659Properties
660----------
661
662Calling :func:`property` is a succinct way of building a data descriptor that
663triggers function calls upon access to an attribute. Its signature is::
664
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700665 property(fget=None, fset=None, fdel=None, doc=None) -> property
Georg Brandl45cceeb2010-05-19 21:39:51 +0000666
667The documentation shows a typical use to define a managed attribute ``x``::
668
Serhiy Storchakae042a452019-06-10 13:35:52 +0300669 class C:
Georg Brandl45cceeb2010-05-19 21:39:51 +0000670 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
675To see how :func:`property` is implemented in terms of the descriptor protocol,
676here is a pure Python equivalent::
677
Serhiy Storchakae042a452019-06-10 13:35:52 +0300678 class Property:
Georg Brandl45cceeb2010-05-19 21:39:51 +0000679 "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 Hettinger632c8c82013-03-10 09:41:18 -0700685 if doc is None and fget is not None:
686 doc = fget.__doc__
Georg Brandl45cceeb2010-05-19 21:39:51 +0000687 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 Hettinger632c8c82013-03-10 09:41:18 -0700693 raise AttributeError("unreadable attribute")
Georg Brandl45cceeb2010-05-19 21:39:51 +0000694 return self.fget(obj)
695
696 def __set__(self, obj, value):
697 if self.fset is None:
Raymond Hettinger632c8c82013-03-10 09:41:18 -0700698 raise AttributeError("can't set attribute")
Georg Brandl45cceeb2010-05-19 21:39:51 +0000699 self.fset(obj, value)
700
701 def __delete__(self, obj):
702 if self.fdel is None:
Raymond Hettinger632c8c82013-03-10 09:41:18 -0700703 raise AttributeError("can't delete attribute")
Georg Brandl45cceeb2010-05-19 21:39:51 +0000704 self.fdel(obj)
705
Raymond Hettinger632c8c82013-03-10 09:41:18 -0700706 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 Brandl45cceeb2010-05-19 21:39:51 +0000715The :func:`property` builtin helps whenever a user interface has granted
716attribute access and then subsequent changes require the intervention of a
717method.
718
719For instance, a spreadsheet class may grant access to a cell value through
720``Cell('b10').value``. Subsequent improvements to the program require the cell
721to be recalculated on every access; however, the programmer does not want to
722affect existing client code accessing the attribute directly. The solution is
723to wrap access to the value attribute in a property data descriptor::
724
Serhiy Storchakae042a452019-06-10 13:35:52 +0300725 class Cell:
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700726 ...
727
728 @property
729 def value(self):
_ = NaNb066edf2017-06-23 11:54:35 +0800730 "Recalculate the cell before returning value"
Georg Brandl45cceeb2010-05-19 21:39:51 +0000731 self.recalc()
_ = NaNb066edf2017-06-23 11:54:35 +0800732 return self._value
Georg Brandl45cceeb2010-05-19 21:39:51 +0000733
734
735Functions and Methods
736---------------------
737
738Python's object oriented features are built upon a function based environment.
739Using non-data descriptors, the two are merged seamlessly.
740
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700741Functions stored in class dictionaries get turned into methods when invoked.
742Methods only differ from regular functions in that the object instance is
743prepended to the other arguments. By convention, the instance is called
744*self* but could be called *this* or any other variable name.
Georg Brandl45cceeb2010-05-19 21:39:51 +0000745
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700746Methods can be created manually with :class:`types.MethodType` which is
747roughly 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
761To support automatic creation of methods, functions include the
762:meth:`__get__` method for binding methods during attribute access. This
763means that functions are non-data descriptors which return bound methods
764during dotted lookup from an instance. Here's how it works::
Georg Brandl45cceeb2010-05-19 21:39:51 +0000765
Serhiy Storchakae042a452019-06-10 13:35:52 +0300766 class Function:
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700767 ...
768
Georg Brandl45cceeb2010-05-19 21:39:51 +0000769 def __get__(self, obj, objtype=None):
770 "Simulate func_descr_get() in Objects/funcobject.c"
Raymond Hettinger0d4497b2017-09-25 01:05:49 -0700771 if obj is None:
772 return self
Mariano Anaya1bced562017-06-05 04:46:50 +0200773 return types.MethodType(self, obj)
Georg Brandl45cceeb2010-05-19 21:39:51 +0000774
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700775Running the following class in the interpreter shows how the function
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700776descriptor works in practice::
Georg Brandl45cceeb2010-05-19 21:39:51 +0000777
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700778 class D:
779 def f(self, x):
780 return x
Raymond Hettinger0d4497b2017-09-25 01:05:49 -0700781
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700782The function has a :term:`qualified name` attribute to support introspection::
783
784 >>> D.f.__qualname__
785 'D.f'
786
787Accessing the function through the class dictionary does not invoke
788:meth:`__get__`. Instead, it just returns the underlying function object::
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700789
Raymond Hettinger0d4497b2017-09-25 01:05:49 -0700790 >>> D.__dict__['f']
791 <function D.f at 0x00C45070>
792
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700793Dotted access from a class calls :meth:`__get__` which just returns the
794underlying function unchanged::
795
Raymond Hettinger0d4497b2017-09-25 01:05:49 -0700796 >>> D.f
797 <function D.f at 0x00C45070>
798
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700799The interesting behavior occurs during dotted access from an instance. The
800dotted lookup calls :meth:`__get__` which returns a bound method object::
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700801
802 >>> d = D()
Raymond Hettinger0d4497b2017-09-25 01:05:49 -0700803 >>> d.f
Georg Brandl45cceeb2010-05-19 21:39:51 +0000804 <bound method D.f of <__main__.D object at 0x00B18C90>>
805
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700806Internally, the bound method stores the underlying function and the bound
807instance::
808
Raymond Hettinger0d4497b2017-09-25 01:05:49 -0700809 >>> d.f.__func__
810 <function D.f at 0x1012e5ae8>
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700811
Raymond Hettinger0d4497b2017-09-25 01:05:49 -0700812 >>> d.f.__self__
813 <__main__.D object at 0x1012e1f98>
Georg Brandl45cceeb2010-05-19 21:39:51 +0000814
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700815If 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 Brandl45cceeb2010-05-19 21:39:51 +0000818
819Static Methods and Class Methods
820--------------------------------
821
822Non-data descriptors provide a simple mechanism for variations on the usual
823patterns of binding functions into methods.
824
825To recap, functions have a :meth:`__get__` method so that they can be converted
Serhiy Storchakad65c9492015-11-02 14:10:23 +0200826to a method when accessed as attributes. The non-data descriptor transforms an
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700827``obj.f(*args)`` call into ``f(obj, *args)``. Calling ``cls.f(*args)``
Georg Brandl45cceeb2010-05-19 21:39:51 +0000828becomes ``f(*args)``.
829
830This chart summarizes the binding and its two most useful variants:
831
832 +-----------------+----------------------+------------------+
833 | Transformation | Called from an | Called from a |
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700834 | | object | class |
Georg Brandl45cceeb2010-05-19 21:39:51 +0000835 +=================+======================+==================+
836 | function | f(obj, \*args) | f(\*args) |
837 +-----------------+----------------------+------------------+
838 | staticmethod | f(\*args) | f(\*args) |
839 +-----------------+----------------------+------------------+
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700840 | classmethod | f(type(obj), \*args) | f(cls, \*args) |
Georg Brandl45cceeb2010-05-19 21:39:51 +0000841 +-----------------+----------------------+------------------+
842
843Static 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
846result, the function becomes identically accessible from either an object or a
847class.
848
849Good candidates for static methods are methods that do not reference the
850``self`` variable.
851
852For instance, a statistics package may include a container class for
853experimental data. The class provides normal methods for computing the average,
854mean, median, and other descriptive statistics that depend on the data. However,
855there may be useful functions which are conceptually related but do not depend
856on the data. For instance, ``erf(x)`` is handy conversion routine that comes up
857in statistical work but does not directly depend on a particular dataset.
858It can be called either from an object or the class: ``s.erf(1.5) --> .9332`` or
859``Sample.erf(1.5) --> .9332``.
860
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700861Since static methods return the underlying function with no changes, the
862example calls are unexciting::
Georg Brandl45cceeb2010-05-19 21:39:51 +0000863
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700864 class E:
865 @staticmethod
866 def f(x):
867 print(x)
868
Shubham Aggarwalabbdd1f2019-03-20 08:25:55 +0530869 >>> E.f(3)
Georg Brandl45cceeb2010-05-19 21:39:51 +0000870 3
Shubham Aggarwalabbdd1f2019-03-20 08:25:55 +0530871 >>> E().f(3)
Georg Brandl45cceeb2010-05-19 21:39:51 +0000872 3
873
874Using the non-data descriptor protocol, a pure Python version of
875:func:`staticmethod` would look like this::
876
Serhiy Storchakae042a452019-06-10 13:35:52 +0300877 class StaticMethod:
Serhiy Storchakadba90392016-05-10 12:01:23 +0300878 "Emulate PyStaticMethod_Type() in Objects/funcobject.c"
Georg Brandl45cceeb2010-05-19 21:39:51 +0000879
Serhiy Storchakadba90392016-05-10 12:01:23 +0300880 def __init__(self, f):
881 self.f = f
Georg Brandl45cceeb2010-05-19 21:39:51 +0000882
Serhiy Storchakadba90392016-05-10 12:01:23 +0300883 def __get__(self, obj, objtype=None):
884 return self.f
Georg Brandl45cceeb2010-05-19 21:39:51 +0000885
886Unlike static methods, class methods prepend the class reference to the
887argument list before calling the function. This format is the same
888for whether the caller is an object or a class::
889
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700890 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 Brandl45cceeb2010-05-19 21:39:51 +0000899
900
901This behavior is useful whenever the function only needs to have a class
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700902reference and does not care about any underlying data. One use for
Raymond Hettinger4a9c6372020-10-24 20:34:39 -0700903class methods is to create alternate class constructors. The classmethod
Georg Brandl45cceeb2010-05-19 21:39:51 +0000904:func:`dict.fromkeys` creates a new dictionary from a list of keys. The pure
905Python equivalent is::
906
Serhiy Storchakae042a452019-06-10 13:35:52 +0300907 class Dict:
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700908 ...
909
910 @classmethod
911 def fromkeys(cls, iterable, value=None):
Georg Brandl45cceeb2010-05-19 21:39:51 +0000912 "Emulate dict_fromkeys() in Objects/dictobject.c"
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700913 d = cls()
Georg Brandl45cceeb2010-05-19 21:39:51 +0000914 for key in iterable:
915 d[key] = value
916 return d
Georg Brandl45cceeb2010-05-19 21:39:51 +0000917
918Now 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
923Using the non-data descriptor protocol, a pure Python version of
924:func:`classmethod` would look like this::
925
Serhiy Storchakae042a452019-06-10 13:35:52 +0300926 class ClassMethod:
Serhiy Storchakadba90392016-05-10 12:01:23 +0300927 "Emulate PyClassMethod_Type() in Objects/funcobject.c"
Georg Brandl45cceeb2010-05-19 21:39:51 +0000928
Serhiy Storchakadba90392016-05-10 12:01:23 +0300929 def __init__(self, f):
930 self.f = f
Georg Brandl45cceeb2010-05-19 21:39:51 +0000931
Raymond Hettinger8d3d7312020-10-23 12:55:39 -0700932 def __get__(self, obj, cls=None):
933 if cls is None:
934 cls = type(obj)
Raymond Hettinger8e5b0fd2020-10-23 18:37:27 -0700935 if hasattr(obj, '__get__'):
936 return self.f.__get__(cls)
937 return types.MethodType(self.f, cls)
938
939The code path for ``hasattr(obj, '__get__')`` was added in Python 3.9 and
940makes it possible for :func:`classmethod` to support chained decorators.
941For 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}'