[Python-Dev] Arbitrary attributes on funcs and methods

Skip Montanaro skip@mojam.com (Skip Montanaro)
Wed, 12 Apr 2000 13:27:38 -0500 (CDT)


    BAW> Functions and methods are first class objects, and they already
    BAW> have attributes, some of which are writable.

    SM> I haven't actually got anything against adding attributes to
    SM> functions (or numbers, if it's appropriate).  Just wondering out
    SM> loud and playing a bit of a devil's advocate.

    BAW> Python 1.6a2 (#26, Apr 12 2000, 13:53:57)  [GCC 2.8.1] on sunos5
    BAW> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    >>>> i = 3.1416
    >>>> dir(i)
    BAW> []

    BAW> Floats don't currently have attributes.

True enough, but why can't they?  I see no reason that your writable
function attributes proposal requires that functions already have
attributes.  Modifying my example, how about:

    >>> l = [1,2,3]
    >>> l.__type__ = "int"

Like functions, lists do have (readonly) attributes.  Why not allow them to
have writable attributes as well?

Awhile ago, Paul Prescod proposed something I think he called a super tuple,
which allowed you to address tuple elements using attribute names:

    >>> t = ("x": 1, "y": 2, "z": 3)
    >>> print t.x
    1
    >>> print t[1]
    2

(or something like that).  I'm sure Paul or others will chime in if they
think it's relevant.

Your observation was that functions have a __doc__ attribute that is being
abused in multiple, conflicting ways because it's the only function
attribute people have to play with.  I have absolutely no quibble with that.
See:

     http://www.python.org/pipermail/doc-sig/1999-December/001671.html

(Note that it apparently fell on completely deaf ears... ;-) I like your
proposal.  I was just wondering out loud if it should be more general.

Skip