[Python-Dev] A syntax for function attributes?

Jeff Epler jepler@unpythonic.net
Wed, 30 Jul 2003 11:20:36 -0500


On Wed, Jul 30, 2003 at 08:38:13AM -0700, Mark Nottingham wrote:
> I'm happy to write a PEP if that will help, but wanted to get a sense 
> of what people's thinking was.

Well, if a function-modifier syntax is adopted (most frequently
discussed in forms like
    def f() [modifier, modifier]: pass
) then a function-attribute modifier could sure be written:
    def attributes(**kw):
        def _attributes(f):
            for k, v in kw.iteritems():
                setattr(f, k, v)
            return f
        return _attributes
and used as follows (my example being of the 'type-hint' variety):
    def sqr(x) [optimize,
                attributes(
                    return_type = float,
                    argument_type = (float,),
                    pure = True)]:
        return x*x
Of course, you could also do this:
    mathfunc = attribute(pure=true, return_type=float, argument_type=(float,))
    def f(x) [optimze, mathfunc]: return x*x

Or the grammar for the modifier-list could be modified to accept 'name =
expr' and 'expr', treating the former as creating a function attribute,
and the latter as giving a modifier function:
    def sqr(x) [optimize,
                return_type = float,
                argument_type = (float,),
                pure = True]:
        return x*x

I don't think I'll be a big user of function attributes, as compared to
function modifiers *cough*classmethod*cough*, so perhaps I'm wrong to
think that the latter should be done as a special case of the former.
Certainly the [attributes()] approach will be harder for a parse-only
tool to take advantage of, especially in the face of things like
'mathfunc = attribute(...)'.  Is this an important feature for function
attributes to have?  What other reasons are there for a distinct syntax?

Jeff