>>> a_string = "this is a string"
>>> a_method = a_string.upper
>>> a_string.upper()
'THIS IS A STRING'
>>> a_method()
'THIS IS A STRING'
>>> a_string.upper
<built-in method upper of str object at 0x7fb01c024760>
>>> a_method
<built-in method upper of str object at 0x7fb01c024760>
>>> repr(a_string.upper) == repr(a_method)
True
>>> a_method is a_string.upper
False
>>> id(a_string.upper)
140394361523072
>>> id(a_method)
140394360882896
The memory addresses in the reprs are identical (0x7fb01c024760) yet they compare false for identity and the results of the id
function are different. What is happening here? I thought that when you make an assignment it just makes the new identifier reference the same piece of memory instead of copying.
Version information:
Python 3.9.6 (default, Jun 30 2021, 10:22:16)
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
This is CPython, if it matters.