python-2.1 function attributes

Tony J Ibbs (Tibs) tony at lsl.co.uk
Fri Jan 26 06:00:32 EST 2001


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".

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"

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.

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.





More information about the Python-list mailing list