[Python-ideas] Object grabbing
Steven D'Aprano
steve at pearwood.info
Sun May 1 21:36:34 EDT 2016
On Sun, May 01, 2016 at 09:25:17PM -0400, Random832 wrote:
> Is there any reason not to simply implement a performance optimization
> for the normal syntax?
Yes. You can't assume that myobject is the same object in both
statements. A silly toy example:
class MyObject:
def deactivate(self):
global myobject
myobject = None
def fire(self):
launch_missiles()
myobject.deactivate()
myobject.fire()
A more realistic case would be if you have a mutable object:
myobject[1].lookup['key'].property.a()
myobject[1].lookup['key'].property.b()
The compiler cannot assume that (myobject[1].lookup['key'].property)
will refer to the same thing in the two calls. But the programmer can
explicitly do so, using either a temporary variable, or the proposed
"using" statement.
--
Steve
More information about the Python-ideas
mailing list