python-2.1 function attributes

Tony J Ibbs (Tibs) tony at lsl.co.uk
Thu Jan 25 09:48:58 EST 2001


Decent syntax for this is obviously hard (else the Pythonlabs people and
their cohorts would already have gotten one). But...

Carel Fellinger proposed:
> why not enhance this like in:
>
>     >>> def f()
>     ...     with:
>     ...         f.__doc__ = 'spam'
>     ...     print f.__doc__
>     ...     f.__doc__ = f.__doc__ + ' and spam'
>     ...
>     >>> f()
>     spam
>     >>> f()
>     spam and spam
>
> and play some tricks to allow "with: suit" only here without it being
> a true keyword.

Hmm. The use of "with" (or "where" or some other word like that - "with"
seems the most obvious, but gets used for rather different purposes in
other languages, I think) is what I'd been thinking about as well.
My suggestion would be more along the lines of::

        def function(lots,of,args) with {type:"silly",
                                         size:32}
           """function(lots,of,args) -- function attribution

           Note that we already get #function.__doc__#
           for free.
           """

The "with" wouldn't be a keyword (in the same sense that "as" isn't in
"import fred as jim"). The "dictionary" syntax follows some of the
suggestions in the PEP, but the "with" makes it feel neater to me.
I'd prefer to leave the quotes off the key names - if we're adding new
syntax, then it should be OK to add a little magic as well, and it reads
a lot better without them. But that's not a point I'd get religious
about.

I definitely prefer to put the stuff before the doc string - otherwise
it doesn't "bind" well to me. The snag is that one would need a
continuation marker to put the "with" on a separate line::

        def function(lots,of,args) \
            with {type:"silly",
                  size:32}

but that's not much of a snag (and I don't mind continuation markers -
comes of learning BCPL early on, I guess).

Mike Romberg pointed out the second PEP syntax:
> def spam():
>     """The doc string."""
>     { 'attribute1' : 'val1', 'attribute2' : val2 }
>     pass

Paul Prescod wrote:
> I proposed a similar syntax to Guido a couple of years ago and he
> pointed out that it is unclear whether "val2" should be evaluated in
the
> runtime context of spam() or in the global context. The former is what
> the syntax suggests but the latter is the only way to have the feature
> work properly.

Hmm. It seems to me that it is reasonable to expect the context to be
whatever the BDFL decides (there, I can't lose now!), but I would
*assume* that it would be explained (as += and its ilk are) in terms of
"this behaves *as if* it were written out longhand in this manner...".

Presumably the "longhand" spelling of::

        def f(a) with {b:jim}:
           """f(a) -- silly example"""
           a = 1

would be::

        def f(a):
            f.__dict__["b"] = jim
            f.__doc__ = """f(a) -- silly example"""
            a = 1

(although, come to think of it, with the advantage that that is (if I
spelt the dictionary name right) an entirely equivalent alternative)

Does our (much beloved) BDFL still have this objection for *any* syntax
expressing these ideas?

Tibs

--
Tony J Ibbs (Tibs)      http://www.tibsnjoan.co.uk/
Give a pedant an inch and they'll take 25.4mm
(once they've established you're talking a post-1959 inch, of course)
My views! Mine! Mine! (Unless Laser-Scan ask nicely to borrow them.)





More information about the Python-list mailing list