When an instance method object is derived from a class method object, the “class instance” stored in __self__ will actually be the class itself, so that calling either x.f(1) or C.f(1) is equivalent to calling$_{f(C,1)}$ where$_{f}$ is the underlying function. definition for a function$_{f()}$ , and$_{x}$is an instance of$_{C}$, calling$_{x.f(1)}$ is equivalent to calling$_{C.f(x,}$ 1) . Note that the transformation from function object to instance method object happens each time the at- tribute is retrieved from the instance. In some cases, a fruitful optimization is to assign the attribute to a local variable and call that local variable. Also notice that this transformation only happens for user-defined functions; other callable objects (and all non-callable objects) are retrieved without trans- formation. It is also important to note that user-defined functions which are attributes of a class instance are not converted to bound methods; this only happens when the function is an attribute of the class. Generator functions A function or method which uses the$_{yield}$ statement (see section The yield statement ) is called a generator function . Such a function, when called, always returns an iterator object which can be used to execute the body of the function: calling the iterator’s$_{iterator.__next__()}$ method will cause the function to execute until it provides a value using the yield statement. When the function executes a $_{return}$statement or falls off the end, a$_{StopIteration}$ exception is raised and the iterator will have reached the end of the set of values to be returned. Coroutine functions A function or method which is defined using async def is called a coroutine function . Such a function, when called, returns a coroutine object. It may contain await expressions, as well as $_{async with}$and$_{async for}$ statements. See also the Coroutine Objects section. Asynchronous generator functions A function or method which is defined using$_{async def}$and which uses the$_{yield}$ statement is called a asynchronous generator function . Such a function, when called, returns an asynchronous iterator object which can be used in an$_{async for}$statement to execute the body of the function. Built-in functions A built-in function object is a wrapper around a C function. Examples of built-in functions are$_{len()}$ and$_{math.sin()}$ ($_{math}$ is a standard built-in module). The number and type of the arguments are determined by the C function. Special read-only attributes: __doc__ is the function’s documentation string, or$_{None}$ if unavailable;$_{__name__}$ is the function’s name;$_{__self__}$ is set to None (but see the next item);$_{__module__}$ is the name of the module the function was defined in or None if unavailable. Built-in methods This is really a different disguise of a built-in function, this time containing an object passed to the C function as an implicit extra argument. An example of a built-in method is$_{alist.append()}$ , assuming alist is a list object. In this case, the special read-only attribute$_{__self__}$ is set to the object denoted by alist . Classes Classes are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that override$_{__new__()}$ . The arguments of the call are passed to$_{__new__()}$ and, in the typical case, to$_{__init__()}$ to initialize the new instance. Class Instances Instances of arbitrary classes can be made callable by defining a$_{__call__()}$ method in their class. Modules Modules are a basic organizational unit of Python code, and are created by the import system as invoked either by the$_{import}$ statement, or by calling functions such as$_{importlib.import_module()}$ and built-in$_{__import__()}$ . A module object has a namespace implemented by a dictionary object (this is the dictionary referenced by the$_{__globals__}$ attribute of functions defined in the module). Attribute references are translated to lookups in this dictionary, e.g.,$_{m.x}$ is equivalent to$_{m.__dict__["x"]}$ . A module object does not contain the code object used to initialize the module (since it isn’t needed once the initialization is done). Attribute assignment updates the module’s namespace dictionary, e.g., m.x = 1 is equivalent to m. __dict__["x"] = 1 . Chapter 3. Data model 22 Calling the asynchronous iterator’s aiterator.__anext__() method will return an awaitable which when awaited will execute until it provides a value using the$_{yield}$ expression. When the func- tion executes an empty$_{return}$statement or falls off the end, a$_{StopAsyncIteration}$exception is raised and the asynchronous iterator will have reached the end of the set of values to be yielded. The Python Language Reference, Release 3.9.5