简体   繁体   中英

Calling a Python function without parentheses

In Python, is there a way to call a function without parentheses?

like

account.add 3000

I want to code a quick app and use it on a Python shell window, and I think it should save time to input values without those parentheses.

You can make use of the eval function in python to convert a string to a function call:

def fun(function, value):
    eval(function + f'({value})')

def add(value):
    x = value
    print(x)

x,y = input().split()

fun(x,y)

Here we take x and y as inputs from the user. x denotes the function to be called and y acts as the parameter. Then we simply call eval and passing it a string to evaluate consisting of the function call as it was with parenthesis.

You can simply put this code in an infinite loop to be able to continuously evaluate functions in this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM