[Tutor] method conflict?

Laura Creighton lac at openend.se
Sun Jul 5 01:25:11 CEST 2015


In a message of Thu, 02 Jul 2015 17:39:12 -0700, "Jim Mooney Py3.4.3winXP" writ
es:
>Okay, it appears the method in a class has its own ID, but all
>instantiations of that method have identical IDs. But what happens if we
>have a huge number of instantiations trying to access the identical method
>at the same time?
>
>class MyClass:
>    def setdata(self, data):
>        self.data = data
>    def getdata(self):
>        print(self.data)
>
>
>>>> MyClass.setdata
><function MyClass.setdata at 0x026CACD8>
>>>> id(MyClass.setdata)
>40676568
>>>> f = MyClass()
>>>> g = MyClass()
>>>> id(f.setdata)
>43576616
>>>> id(g.setdata)
>43576616
>>>> d = MyClass()
>>>> id(d.setdata)
>43576616
>
>
>-- 
>Jim

Implementation dependent.  What Jython does and what CPython does are
not the same thing here.

You need, if at all possible, to flush your brain of the C and C++ ish
idea that through id I can find the exact chunk of memory where this value 
is stored, if you ever want your code to run other places than in CPython.
Python does not have any idea of 'the exact chunk of memory
where something is stored', though that is, indeed how CPython implements
it.

The language only promises that you will get an integer which is unique
and constant for the lifetime of an object.  If your Python uses a GC
that relocates objects, then one of the things that the GC will have to
do is guarantee that the moved object has the same id as it did when
it was somewhere else.  But it still will be somewhere else.

Laura


More information about the Tutor mailing list