Why we will use obj$func() often

Donn Cave donn at u.washington.edu
Fri Apr 23 18:34:57 EDT 2004


In article <lkfic.29767$dZ1.17301 at fed1read04>,
 "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 sep~rate.

I don't think a closure variable prefix sounds very good.

What you're doing there looks to me a bit like Objective
CAML's ref type.  "a = ref 0" is a mutable container for an
immutable integer.  It's just syntactic sugar for a mutable
record;  the sugar is the assignment a := 1, and then there's
some special dereference notation -- a := !a + 1, something
like that, I forget.

There could be a case for a mutable-container-of-one-item
type in Python, with or without special syntax.  Without
the syntax, it's still mildly interesting because it expresses
intent - both to the human reader, and to other software
components which should not be invited to delete or add
items etc.

class Ref:
        def __init__(self, a):
                self.value = a
        def __setattr__(self, a, v):
                if a == 'value':
                        self.__dict__['value'] = v
                else:
                        raise AttributeError, a

var = Ref(0)

var.value = var.value + 1


I personally think syntactic sugar wouldn't add any real
value to that, but that just seems to be one of those
matters of taste, where some people like languages like
Pe... oops.

   Donn Cave, donn at u.washingtno.edu



More information about the Python-list mailing list