function attributes are like function objects

Fredrik Lundh fredrik at effbot.org
Fri Feb 2 15:54:57 EST 2001


Geoffrey Gerrietts wrote:
> Has the addition of function attributes included the addition of an
> introspection mechanism to allow the function access to its own
> attributes? In other words, can a function use its attributes to
> provide the moral equivalent of a static variable, or a closure?

nope.

> Or are function attributes pretty much accessible only to those
> who know the function by name?

exactly.

on the other hand, the programmer defining a function
often knows what it's called, so things like the following
will work (if spam is defined in the global namespace,
and as long as nobody else overloads spam with some-
thing else):

    def spam(x):
        # fetch attribute via global namespace
        # lookup and attribute lookup
        print spam.x
    spam.x = "hello"

but if that's what you want, you can of course use
a global instead:

    def spam(x):
        # fetch attribute via global namespace lookup
        print spam_x
    spam_x = "hello"

Cheers /F





More information about the Python-list mailing list