One Python 2.1 idea

Roy Smith roy at panix.com
Tue Dec 26 08:55:51 EST 2000


Thomas Wouters <thomas at xs4all.net> wrote:
> You *can* speed it up, by doing 'x.foo = x.foo' somewhere early in 
> your call chain :) I experimented with this sometime during 2.0 
> development, but doing this for every call (by adjusting 
> instance_getattr2 to store bound methods in the instance dict after 
> creating them) proved only a marginal (and disputable) improvement, 
> and broke the pickle modules (because bound methods can't be 
> pickled.) And I'm not sure if the benifit is large enough to actually 
> build a framework of caching (which Python lacks entirely, currently) 
> and use it in the few places that might use it.

I do this on occasion when I have deeply nested data structures (not 
classes) and I make frequent use of a value or method burried deep in 
the chain.  Depending on how deep the chain gets, "frequent" might mean 
"more than once" :-)  For example, I might do:

x.port = x.page.frame.server.connection.input.socket.port

but I do it because I think it makes the code easier to read (and 
certainly easier to type!), not because I'm particularly worried about 
how efficient it is.



More information about the Python-list mailing list