Silly function call lookup stuff?

Dan Sommers me at privacy.net
Tue Sep 27 21:48:25 EDT 2005


On Wed, 28 Sep 2005 00:38:23 +0200,
Lucas Lemmens <llemmens at gmx.net> wrote:

> On Tue, 27 Sep 2005 13:56:53 -0700, Michael Spencer wrote:

>> Lucas Lemmens wrote:

>>> Why isn't the result of the first function-lookup cached so that
>>> following function calls don't need to do the function-lookup at
>>> all?

>> I guess because the function name may be re-bound between loop
>> iterations.  Are there good applications of this?  I don't know.

> Yuk I'd hate that. I think it would be extremely rare.

With duck typing, I think it would be fairly common:

    def process_list_of_things_to_process( list_of_things_to_process ):
        for x in list_of_things_to_process:
            x.process( )

As long as list_of_things_to_process is sufficiently list-like, this
function has no way of knowing what's in it, or what any particular
x.process function might do.

Think of the possibilities if the list is really the output of some
generator with a feedback mechanism that can add new elements to the end
of the list before the list is exhausted.  Yes, this case is a stretch;
no, I can't say that I'll never implement anything like it, or that I
wouldn't marvel at the elegance of such an implementation.

> Would the myLocalFunc = hisRemoteFunc optimization break in such a
> case?

Trying to cache x.process in the above loop would be nothing short of a
complete disaster.

>> Would you apply this optimization to all lookups in outer scopes, or
>> just callables?  Why? ;-)

> Hmmm callables have probably the highest chance of being recalled.

    def print_the_results( list_of_things_with_results ):
        for x in list_of_things_with_results:
            print x.results

Regards,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>



More information about the Python-list mailing list