Access function defaults

Fredrik Lundh fredrik at pythonware.com
Tue Feb 15 17:03:16 EST 2005


James Stroud wrote:

> By the way, I already know a million "better" ways to do this, so no
> need for the "why would you want to do that?" responses.

why not?

(if you really want to know how this works, study the source code for
the "getargspec" function in the inspect module.  or easier, you can just
use that function to fetch the default:

    def getdefault(function, name):
        import inspect
        args, varargs, varkw, defaults = inspect.getargspec(bob)
        first = len(args) - len(defaults)
        pos = args.index(name)
        if pos >= first:
            return defaults[pos - first]
        return None # no default

    print getdefault(bob, "wives")

)

</F> 






More information about the Python-list mailing list