Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
instance methods
#7
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)
179605616
So 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
Reply


Messages In This Thread
instance methods - by mim - Mar-28-2021, 05:01 AM
RE: instance methods - by deanhystad - Mar-28-2021, 05:30 AM
RE: instance methods - by mim - Mar-28-2021, 05:39 AM
RE: instance methods - by ndc85430 - Mar-28-2021, 06:45 AM
RE: instance methods - by deanhystad - Mar-28-2021, 07:02 AM
RE: instance methods - by mim - Mar-28-2021, 07:21 AM
RE: instance methods - by snippsat - Mar-28-2021, 12:34 PM
RE: instance methods - by deanhystad - Mar-28-2021, 03:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Copying methods to effect the new owner instead of the old instance Daniel285 2 1,053 Jun-03-2024, 07:58 AM
Last Post: Gribouillis
  instance methods invokation mim 3 3,469 Apr-04-2021, 08:18 AM
Last Post: Gribouillis
  instance methods sharing addresses mim 1 2,962 Mar-28-2021, 05:22 AM
Last Post: deanhystad
  Class object instance. Link instance attribute to class. Can it be done easier. Windspar 7 5,722 Dec-03-2018, 11:16 PM
Last Post: Windspar
  How to check if class instance exists in a list of class instance objects? sonicblind 23 26,888 May-27-2018, 05:44 AM
Last Post: buran
  Python3x running only with one instance (how can I prevent too many running instance) harun2525 5 21,438 Jul-21-2017, 07:36 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020