Discussion:
[Tutor] method conflict?
Jim Mooney Py3.4.3winXP
2015-07-03 00:39:12 UTC
Permalink
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
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Cameron Simpson
2015-07-03 01:46:26 UTC
Permalink
Post by Jim Mooney Py3.4.3winXP
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
Post by Jim Mooney Py3.4.3winXP
self.data = data
print(self.data)
MyClass.setdata
<function MyClass.setdata at 0x026CACD8>
id(MyClass.setdata)
40676568
f = MyClass()
g = MyClass()
Two instances of the class.
Post by Jim Mooney Py3.4.3winXP
id(f.setdata)
43576616
id(g.setdata)
43576616
Two references to the same method obtained from the class.
Post by Jim Mooney Py3.4.3winXP
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 <***@zip.com.au>
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-07-03 02:00:54 UTC
Permalink
Post by Jim Mooney Py3.4.3winXP
Okay, it appears the method in a class has its own ID, but all
instantiations of that method have identical IDs.
I'm not sure what you mean by this. *All* objects have their own ID, and
instantiations of methods *don't* have identical IDs, although it may
appear so at first glance (see further below).

py> class C:
... def method(self):
... return 23
...
py> c = C()
py> x = c.method # grab a reference to the method
py> y = c.method # and another one
py> print(id(x), id(y))
3082036364 3082100428

*But* that's actually an implementation detail. It just so happens that
the way CPython currently works, each time you look up a method you get
a new instance. Technical details are technical, and involve the
descriptor protocol, which you don't need to understand. I'm happy to go
into detail if you like, but for now let's just skip over it and just
call it "magic".

There's nothing in the language specification (that I know of) which
*promises* that x and y must be separate objects with different IDs.

The underlying function object, retrieved directly from the class
__dict__, *is* guaranteed to always give the same object:

py> a = C.__dict__['method']
py> b = C.__dict__['method']
py> print(id(a), id(b))
3083756356 3083756356

That's the way dictionaries work.
Post by Jim Mooney Py3.4.3winXP
But what happens if we
have a huge number of instantiations trying to access the identical method
at the same time?
What of it? I don't understand what you think the potential problem is.
Regardless of whether you access the method wrapper, or the underlying
function, calling it from multiple places is allowed. Each function call
is independent of the others.

Naturally, the *action* of the function may not be safe to call twice.
(It might, say, try to delete the same file twice, and the second time
you get an error because the file no longer exists.) But the function
calling infrastructure is safe to do multiple times:

len(a) # Safe to call it once.
len(b) # and still safe to call it twice
LEN = len; LEN(c) # even if you use a different name
Post by Jim Mooney Py3.4.3winXP
self.data = data
print(self.data)
MyClass.setdata
<function MyClass.setdata at 0x026CACD8>
In Python 3, looking up MyClass.setdata returns the underlying function
object. But in Python 2, it actually returns an "unbound method" object,
which is a wrapper around the underlying function. I mention this only
for completion, in practice it makes little or no difference.
Post by Jim Mooney Py3.4.3winXP
id(MyClass.setdata)
40676568
f = MyClass()
g = MyClass()
id(f.setdata)
43576616
id(g.setdata)
43576616
d = MyClass()
id(d.setdata)
43576616
You are misinterpreting what you are seeing. Python is free to reuse
IDs. CPython does re-use them, IronPython and Jython do not.

When you run this line of code:

id(f.setdata)

at least five things happen:

- Python does an attribute search for "setdata" starting with f;

- it finds the *function* f inside the class __dict__;

- because functions are descriptors ("magic"), Python creates a *method*
wrapper around the function, and returns that;

- the method gets passed to the id() function, which determines the ID
(in this case 43576616) and returns that;

- the method object is then garbage collected, which frees up the ID to
be re-used the next time you create a method.


If you did the same thing in (say) Jython, you would get very different
Post by Jim Mooney Py3.4.3winXP
id(f.setdata)
43
Post by Jim Mooney Py3.4.3winXP
id(g.setdata)
44
Post by Jim Mooney Py3.4.3winXP
id(d.setdata)
45

(the actual IDs may vary).

But *most of the time* none of this has the least bit of importance to
your code. Whether you get the same method object or different method
objects is more or less irrelevant to how your code works. The exception
is when you start looking at introspection functions like id(), or
testing for object identity ("is this the same object?") using `is`.
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Mark Lawrence
2015-07-03 04:35:30 UTC
Permalink
Post by Jim Mooney Py3.4.3winXP
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?
I understand that Python "just works". What do you think happens?
Post by Jim Mooney Py3.4.3winXP
self.data = data
print(self.data)
I'd say the only conflict here is writing unneeded boilerplate code in
Python :)
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-07-03 09:21:16 UTC
Permalink
Post by Jim Mooney Py3.4.3winXP
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?
They all execute the method.

Its no different to a global function. What happens if lots
of objects, or even threads, access a global function? They
all execute the same code, each within their own environment.
It's the same with methods.

I'm not sure why you think there would be an issue?

One issue that may be confusing things is when you say
"at the same time". Now in principle they could all be running
concurrently since they never modify the function they only
"read" it but in practice CPython serializes those calls
anyway so only one call is active at any one time. But
there is no real reason why that should be the case.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-07-04 23:25:11 UTC
Permalink
In a message of Thu, 02 Jul 2015 17:39:12 -0700, "Jim Mooney Py3.4.3winXP" writ
Post by Jim Mooney Py3.4.3winXP
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?
self.data = data
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
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...