Mar-28-2021, 12:34 PM
mi Wrote:and i believe the usage of "self" in creating class is to send object address to class and then method execute in place in class not in object and the result will send to caller.I don't think underlaying object memory address should be used when try to explain how a class work.
In Python object can point to same memory address,this can happens everywhere.
>>> s = 'hello' >>> d = s >>> id(s) 179605616 >>> id(d) 179605616So here both
s
and d
name tag to same memory address.It will soon be complicated.
class C: def method(self, arg): return f'I am method {arg}' c1 = C() c2 = C()
>>> id(c1.method) 179587328 >>> id(c2.method) 179587328 >>> >>> id(c1.method('taxi')) 179549648 >>> id(c2.method('bus')) 179588656 >>> >>> c1.method <bound method C.method of <__main__.C object at 0x000000000AA193A0>> >>> c2.method <bound method C.method of <__main__.C object at 0x000000000AA10B50>>So method without calling point to same memory address.
When call it with argument the memory address will be different.
Last we see a different memory address