The output problem of python print function

class Foo(object):
    def f(self):
        pass
        
a=Foo()
print(id(Foo.f),id(a.f))
print(a.f,id(Foo.f),id(a.f))
print(str(a.f),id(Foo.f),id(a.f))

output result:

2005918566192 2005885408456
<bound method Foo.f of <__main__.Foo object at 0x000001D30A108B00>> 2005918566192 2005885410056
<bound method Foo.f of <__main__.Foo object at 0x000001D30A108B00>> 2005918566192 2005885408456

question: why are the three outputs different?
from the result, we can see that the _ _ str__ method of the object is not called by itself during print, so what"s the use of _ _ str__?

Mar.15,2021

it is not clear why the results of the three print are different. It is estimated that A.F occurs automatically in memory.
as to why _ _ str__, is not called because the code does not require a description of a, all that is returned is the description of method f. In the
output, the content after "of" in angle brackets is for the machine, so the _ _ repr__ method of the Foo class is called instead of _ _ str__


Why is Foo.f different from a.f ? To put it simply: Foo.f accepts a parameter, while a.f has been bound to Foo.f , the first parameter is a , so Foo.f and a.f are different. a.f is the version of Foo.f bound to the parameter, and Foo.f = = a.f. binding function _ <

official documents:

When an instance method object is created by retrieving a user-defined function object from a class via one of its instances, its _ self_ attribute is the instance, and the method object is said to be bound. The new method's _ func_ attribute is the original function object.
Menu