[Tutor] method conflict?

Cameron Simpson cs at zip.com.au
Fri Jul 3 03:46:26 CEST 2015


On 02Jul2015 17:39, Jim Mooney Py3.4.3winXP <cybervigilante at gmail.com> wrote:
>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?

Um, they all get given access to the method? I'm not sure what you think should 
be happening here:

>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()

Two instances of the class.

>>>> id(f.setdata)
>43576616
>>>> id(g.setdata)
>43576616

Two references to the same method obtained from the class.

>>>> d = MyClass()
>>>> id(d.setdata)
>43576616

And a third.

Steven D'Aprano posted a decent explaination in your other thread ("Are the 
methods in a class copied or just linked to?"); it would have been helpful if 
your post had stayed in that thread - it is essentially the same discussion.

So all your "d.setdata" expressions consult various places in order to find the 
method definition, and they all find it in the class. And so you are given the 
same method every time, and thus the same id.

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Tutor mailing list