Descriptors and Dotted Lookups

suggest change

Descriptors are objects that are (usually) attributes of classes and that have any of __get__, __set__, or __delete__ special methods.

Data Descriptors have any of __set__, or __delete__

These can control the dotted lookup on an instance, and are used to implement functions, staticmethod, classmethod, and property. A dotted lookup (e.g. instance foo of class Foo looking up attribute bar - i.e. foo.bar) uses the following algorithm:

  1. bar is looked up in the class, Foo. If it is there and it is a Data Descriptor, then the data descriptor is used. That’s how property is able to control access to data in an instance, and instances cannot override this. If a Data Descriptor is not there, then
  2. bar is looked up in the instance __dict__. This is why we can override or block methods being called from an instance with a dotted lookup. If bar exists in the instance, it is used. If not, we then
  3. look in the class Foo for bar. If it is a Descriptor, then the descriptor protocol is used. This is how functions (in this context, unbound methods), classmethod, and staticmethod are implemented. Else it simply returns the object there, or there is an AttributeError

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Classes:
*
* Descriptors and Dotted Lookups

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve