Syntax Suggestion: Pass Function Definition as Argument

Stephen Waldron mail.stephen.waldron at gmail.com
Thu Nov 7 08:36:04 EST 2019


Hi, I'm new to the group and to Python, so forgive me if I make any faux-pas here. As I can tell, the only way to pass a function as an argument is to reference its name as follows:

def foo1(message):
    print(message)

def foo2(foo, message):
    print("Your function says:")
    foo(message)


>>foo2(foo1, "Hello World!")
Your function says:
Hello World!


This is how it is at the moment, however it may be more agreeable, especially if that is the only purpose of the function, for python users to be able to define new functions inside of function calls. The way I propose this may occur is using similar syntax as control structures, as exampled below:

def foo2(foo):
    print("Message to print:", end = " ")
    foo()


>>foo2():
....print("Hello World!")

Message to print: Hello World!


In this proposal, it should be noted, that unlike control structures, the brackets are required for the function call. The "suite" or "annex" is defined as a function object by the compiler and passed inside the call. Here are some other examples of the proposal.



*Non-Function Arguments with Annexed Functions*
Non-Function Arguments should be placed in the brackets before annexed functions, and this should be reflected in the parameter order. If they are not, they won't be assigned to, which would cause an error unless a default value is given. These parameters could be available for passing in the annexed function if mentioned when the paramaeter is called.

def myFoo (num1, num2, foo):
    return foo (num1, num2)


>>myFoo(20, 30):
....return num1 + num2

50


*Annexing Multiple Functions or Out of Order Annex*
To specify the order in which functions are annexed, or annex multiple functions, one could use the parameter name before the specific suite.

class Book:
    def __init__(self, name, author):
        self.name = name
        self.author = author


def doToBooks (bookList, first, then, book = None):
    
    for book in bookList
        first(book = book)
        then(book = book)

myBooks = [
    Book("The Way of the World", "Lucy Cole"),
    Book("To Live or To Love", "Georgio Dunham"),
    Book("Twelve Days of Success", "Anita Duvette")
]

doToBooks (myBooks):
    print(book.name)
then:
    print(" - " + book.author + "\n")



>
The Way of The World
 - Lucy Cole

To Live or To Die
 - Georgio Dunham

Twelve Days of Success
 - Anita Duvette
        
    

This proposal does not detract from the core appearance and appeal of Python while creating an option for users and module makers who want themselves or the users of their modules not to have to previously define functions in order to use the code. This would make such code more aesthetic and compact, and take away the need for defining a new function, name and all, specifically for such which ultimately would be used only once as an argument. 
   


More information about the Python-list mailing list