Why we will use obj$func() often

David MacQuigg dmq at gain.com
Fri Apr 23 20:17:22 EDT 2004


On Fri, 23 Apr 2004 13:50:24 -0700, "Mark Hahn" <mark at prothon.org>
wrote:
>"Mel Wilson" <mwilson at the-wire.com> wrote in message
>news:ulXiAls/KH1K089yn at the-wire.com...
>
>> It can, if you spell '&' differently:
>>
>> def getfunc():
>>     def count (n=[0]):
>>         n[0] += 1
>>         print n[0]
>>     return count
>>
>> c = getfunc()
>> c()
>> c()
>> c()
>
>True.  A bit inelegant, but then so is &.
>
>Would you trade the "mutable integer" and "count(n=[0])" kludges for a
>solution like &count ?  Does anyone agree with me that a "closure variable
>prefix" is more explicit and readable?
>
>Let's seperate the aesthetic issues of using "Perl-like" symbols from the
>"closure variable prefix" functionality for a moment.  I'd like feedback on
>both issues, but try to keep them seperate.

We need to see what the problem is before we can judge a solution.
Just showing a code snippet and saying "do this" is not a good
statement of the problem.  As you have seen, there are simple elegant
solutions that don't follow your code line-for-line, and there are
inelegant solutions that are very close.  Both categories seem to
solve what we think the problem is, but you may have another problem
in mind.  A use-case would be helpful.

As an alternative, we could work with a clear statement of the
problem, something like:  The proposed syntax will allow assigning
values to (not just referencing) variables in a scope surrounding a
nested function.  This will be useful when the existing alternatives
are not appropriate:
1) Attach the variable to an instance.
2) Make the variable global.
3) Add the variable to the function argument list.
4) ...

I've probably mistated it, but you see what I'm getting at, something
that de-couples the user-level problem from the particular mechanisms
of the language.  Trying to reproduce these mechanisms in Python will
be the road to Per ... dition. :>)

-- Dave




More information about the Python-list mailing list