python-2.1 function attributes

Michael Hudson mwh21 at cam.ac.uk
Fri Jan 26 07:43:43 EST 2001


On Fri, 26 Jan 2001, Tony J Ibbs (Tibs) wrote:

> Michael Hudson wrote:
> > Ooh, I've come up with a strawman!
> 
> I'm ignoring his first option, since I think the double indent will doom
> it, regardless of other considerations, and the variant is more
> interesting:
> 
> > def f(a, b, c) with publish = 1; secure = 0:
> >     print "bobbins"
> >
> > Then you use backslashes when the attribute or argument list get long.
> >
> > def f(long_name, even_longer_name, still_longer_name) \
> >         with publish = 1; secure = 0
> >              grammar = f_grammar:
> >     print "bobbins"
> >
> > Thoughts?
> 
> My first thought is that the examples need to have doc strings to make
> it a bit more obvious how they work in practice. Also, I think it
> needs a ";" after "secure=0" to be "consistent".

Like after the b = 0 in

    a = 0; b = 0
    c = 0

?  But maybe you're right.

> Of course, we already have ways in Python to allow continuation over
> lines and indenting for a list of items - and I'd have said the obvious
> one to use here *was* the dictionary specification, which takes us back
> to:
> 
>  def f(long_name, even_longer_name, still_longer_name) \
>          with {publish : 1, secure : 0,
>                grammar : f_grammar}:
>      """and here is the doc string."""
>      print "bobbins"

Yes, I've realised this since posting.  But this *isn't* quite dictionary
notation: that would be

 def f(long_name, even_longer_name, still_longer_name) \
         with {"publish" : 1, "secure" : 0,
               "grammar" : f_grammar}:
     """and here is the doc string."""
     print "bobbins"

I'm not sure I like this inconsistency.

> But a better result might be to use a compromise, of sorts, and
> "tupleise" it, with syntax similar to an argument list:
> 
>  def f(long_name, even_longer_name, still_longer_name) with \
>          (publish=1, secure=0, grammar=f_grammar):
>      """and here is the doc string."""
>      print "bobbins"
> 
> - this allows the user to "forget" it's a dictionary, and doesn't
> require that (slightly) awkward mixing of () and {} in the same
> statement. In fact this is my favourite so far. And it's close to
> current Python layout rules, which must be a Good Thing.

The killer for this is as I'm concerned is that I'd read this as exposing
publish, secure and grammar inside f, not outside it (kind of like &aux
variables in Common Lisp if you know them).

> I'm still a little concerned about the use of "with" as the keyword,
> though, since it has precedent for uses where the value being defined is
> a "local" constant - but on the other hand I can't think of a better
> word at the moment.

How about "having"?

def func(param) having (publish=1, secure=0):
    """ docco """
    print "bobbins"

to emphasize the attribute-ness one could use a dot prefix:

def func(param) having (.publish=1, .secure=0):
    """ docco """
    print "bobbins"

I think I like this (though I'm not sure about the dot prefix).  I might
see if I can whip up a patch...

Cheers,
M.




More information about the Python-list mailing list