feature request: parameter functions

Steven D. Arnold stevena at neosynapse.net
Fri Aug 17 22:15:10 EDT 2001


Hi gang,

A feature idea for Python.  Right now, you can specify default
parameters in functions and these parameters can be functions, but
this is of limited value, since the function is evaluated at the time
the def statement is initially executed.  I'd like these function
parameters to be dynamically evaluated, and I'd like to take it a step
farther so that it's not strictly a default parameter issue.
Specifically, I'd like to be able to pass any of the parameter values
to the function if they WERE specified.

Why is this desirable?  It allows you to clean up parameters, ensuring
they are exactly what you expect, perhaps raising an exception if not,
and focusing your function on exactly what it is supposed to do,
without worrying about checking and converting parameters.  Python
largely avoids all the result-checking of C with exceptions; why not
do away with all the type-checking (or object capability-checking)
code at the top of many functions, that just serves to dilute the
simple task of your function? Furthermore, this is a relatively easy
way to get something kind of like type-checking into Python without
going all the way to allowing optional declaration of type for a
variable.

Some example code:

    def foo(a, b, c=myfunc(c)): [...]

    def myfunc(param):
        if not param: return myobject(0)
        if type(param) == IntType:
            return myobject(param)
        elif type(param) == InstanceType:
            return param
        else:
            raise TypeError, 'parameter must be an instance of myobject or an Integer'

Note that a parameter function must return some value or a TypeError
(not enough arguments) is raised.  Any number of parameters may be
passed to a parameter function, including the values passed to the
function itself.  So if the caller wrote something like:

    foo(1, 2, 3)

Then the integer 3 would be passed to myfunc.

All this is really no different than code like this:

    def foo(a, b, c=None):
        c = myfunc(c)

...but it's cleaner, especially for functions with many parameters.
Since function parameters as I described them translate into the
above, it should be easy to translate such headers in bytecode to the
above; thus, internally, function-call semantics need not change. When
def is evaluated, anything with a function as a default value receives
None as the internal default, and the parameter function is called
first thing inside the function body.

One other rule to make things simple: let l be a parameter in a
function that is associated with a parameter function, and m another
parameter associated with a parameter function later in the parameter
list.  If m is a parameter to l's parameter function, the raw value is
used; that is, if a value was passed for m, the value before it is
processed by m's parameter function is used, or None if no value was
passed.

Thoughts, comments?
            
----------------------------------------------------------------------
Steven D. Arnold                                  stevena at neosynapse.net
AIM: abraxan                                               ICQ: 73804392
~~~~~~~~~~~~~~~~~~~~~~~~   There is no spoon.   ~~~~~~~~~~~~~~~~~~~~~~~~





More information about the Python-list mailing list