Syntax Suggestion: Pass Function Definition as Argument

Stephen Waldron mail.stephen.waldron at gmail.com
Thu Nov 7 12:10:06 EST 2019


Thanks Antoon. I do suppose that it is kind of wrong to say the only way is to "reference its [the function's] name" as an argument, however the point I was trying to make was that it isn't possible to pass a function that is either not in some way previously defined or a reference to something previously defined.

In your example, you still make reference to the existing function `truediv` from the `operator` library, even though the `partial` function does create a new function.

What I'm aiming for is the ability to, within a function call, pass a suite that would be there automatically defined by the compiler/interpreter. Another comment did mention lambda functions, which does to some degree provide that capability, but is restricted to well, lambda functions (only expressions, not statements).

Your example does however suggest the possibilities of a function or expression that creates functions. For example, the `function` expression in JavaScript.

```
    //Definition
    function myFoo (str1, str2, foo){
        console.log( foo(str1), foo(str2) );
    }


    //Call
    myFoo ("hello", "world!", function(str){

        str[0] = str[0].toUpper();
        return str;

    });


Output:
Hello World!
```

However it does not seem that there would be any such expression in Python as is. In python, this code would be written:


```
    #Definition
    def myFoo (str1, str2, foo):
        print( foo(str1), foo(str2) )


    #Call
    def other_foo(str):
        str = list(str)[0].upper() + str[1:]
        return str

    myFoo ("hello", "world!", other_foo)

```


Here is it rewritten using the proposal:
```
    #Definition
    def myFoo (str1, str2, foo, str = " "):
        print( foo(str = str1), foo(str = str2) )


    #Call
    myFoo ("hello", "world!"):
        str = list(str)[0].upper() + str[1:]
        return str

```

Of course this example presents only a minor, though you can see the difference in the call, in which no new function needs be referenced.


More information about the Python-list mailing list