This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: __self__ on built-in functions is not as documented
Type: behavior Stage: needs patch
Components: Documentation Versions: Python 3.10, Python 3.9, Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Arfrever, Jim Fasarakis-Hilliard, SpecLad, anacrolix, docs@python, eric.araujo, eryksun, ezio.melotti, michael.foord, r.david.murray, terry.reedy, vaultah
Priority: normal Keywords:

Created on 2012-02-13 19:05 by SpecLad, last changed 2022-04-11 14:57 by admin.

Messages (6)
msg153287 - (view) Author: Роман Донченко (SpecLad) * Date: 2012-02-13 19:05
The language reference says this in section 3.2:
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() <...> Special read-only attributes: <...> __self__ is set to None (but see the next item) <...>.

That is not the case:

ActivePython (ActiveState Software Inc.) based on
Python 3.2.2 (default, Sep  8 2011, 10:55:13) [MSC v.1500 64 bit (AMD64)]  
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> len.__self__
<module 'builtins' (built-in)>
>>> open.__self__
<module 'io' (built-in)>
>>> import math
>>> math.sin.__self__
<module 'math' (built-in)>
msg153288 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-02-13 19:20
It looks like this changed between 2.x and 3.x but the docs were not updated.  None makes more sense than the module as __self__, though, so perhaps it is actually a bug.  Then, again, since Python functions don't have a __self__, the __self__ of built-in functions seems like an anomaly to begin with...
msg153298 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2012-02-13 20:39
It's nicer for introspection if __self__ is None on builtin functions. But fixing the docs is easier (and more backwards compatible).
msg153610 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-02-17 23:02
Python-coded functions do not have .__self__.
>>> def f(): pass

>>> f.__self__
AttributeError: 'function' object has no attribute '__self__'

Unbound builtin methods, which are simply builtins functions attached to a class, do not have .__self__
>>> list.__len__.__self__
AttributeError: 'wrapper_descriptor' object has no attribute '__self__'

So it makes no sense to me that builtin non-method functions should have this attribute.

"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."

should have 'method' replaced with 'instance method' as it is only talking about instance methods, as the term is used in the rest of the section. Or this section should be deleted as it duplicates the previous Instance Method section. Or it should be revised to actually discuss unbound builtin methods.
msg153620 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-02-18 01:22
I think that functions in C modules are implemented as methods of module objects, which would explain why len.__self__ is builtins.
msg244963 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2015-06-07 18:10
In Python 2 [Py_InitModule4][1] optionally allows setting __self__ on module functions, but no module in the standard library actually uses this. It's always None. This is no longer optional with Python 3's [PyModule_Create][2]. Built-in module functions instantiated the normal way can be considered as methods of the module in which they're defined. However, some modules may specially instantiate functions for which __self__ is None, such as codecs.strict_errors.

    >>> codecs.strict_errors.__self__ is None

Date User Action Args
2022-04-11 14:57:26adminsetgithub: 58211
2020-11-07 19:53:05iritkatrielsetversions: + Python 3.8, Python 3.9, Python 3.10, - Python 3.2, Python 3.3
2017-03-20 17:02:00Jim Fasarakis-Hilliardsetnosy: + Jim Fasarakis-Hilliard
2015-09-02 18:13:56serhiy.storchakalinkissue23639 dependencies
2015-06-07 18:10:23eryksunsetnosy: + eryksun
messages: + msg244963
2015-06-07 15:05:10vaultahsetnosy: + vaultah
2012-10-02 05:20:45ezio.melottisetstage: needs patch
2012-02-18 18:42:05Arfreversetnosy: + Arfrever
2012-02-18 10:41:37ezio.melottisetnosy: + ezio.melotti
2012-02-18 01:22:21eric.araujosetnosy: + eric.araujo
messages: + msg153620
2012-02-18 01:07:32anacrolixsetnosy: + anacrolix
2012-02-17 23:02:55terry.reedysetnosy: + terry.reedy
messages: + msg153610
2012-02-13 20:39:54michael.foordsetnosy: + michael.foord
messages: + msg153298
2012-02-13 19:20:26r.david.murraysetnosy: + r.david.murray

messages: + msg153288
versions: + Python 3.3
2012-02-13 19:05:34SpecLadcreate