0

Suppose the code below:

from functools import partial
import random

def integer(min=1, max=10):
    return random.randint(min, max)

def double(min=1, max=10):
    return random.uniform(min, max)

if __name__ == '__main__':
    p1 = partial(integer, 5, 10)
    p2 = partial(double, 5, 10)
    for f in [p1, p2]:
        f() # I'd like to know if there's a different way to call this like `call(f)` or something

As mentioned in the comment, I'd like to know if there's a way to call f without using parentheses. One step further, suppose I can call f without using parentheses, if I would like to pass additional parameters to f, how do I go about it (like call(f, additional_param_1, additional_param_2))?

Thank you in advance for your answers!

9
  • 1
    I get a TracebackError when running your code on Line 14. Commented Aug 7, 2019 at 1:33
  • 1
    Why? Are you just playing with Python syntax, or do you have some concrete reason to do this? Commented Aug 7, 2019 at 1:33
  • 2
    There is f.__call__() but I doubt that's what you're after. Why do you want to be able to do call(f, arg1, arg2)? What problem are you really trying to solve, because there probably is a way that doesn't involve a call() function - although you could write such a function quite easily yourself. Commented Aug 7, 2019 at 1:41
  • 1
    @Cygnus that line should be p1 = partial(integer, 5, 10) Commented Aug 7, 2019 at 1:44
  • 2
    @user1330974 no, there isn't. There used to be an apply function in Python 2, but even then it was considered obsolete probably decades ago. Why would you ever need this? Commented Aug 7, 2019 at 2:10

1 Answer 1

1

Not in base Python, but you can easily write call() yourself:

from functools import partial
import random

def integer(min=1, max=10):
    return random.randint(min, max)

def double(min=1, max=10):
    return random.uniform(min, max)

def call(f, *args, **kwargs):
    return f(*args, **kwargs)

if __name__ == '__main__':
    p1 = partial(integer, 5, 10)
    p2 = partial(double, 5, 10)
    for f in [p1, p2]:
        call(f)

Note: you have a typo in your example, you're calling partial on int, but your function is called integer. (neither is a very good name and I think you're trying to solve a problem that you're not stating, that has a better solution)

Sign up to request clarification or add additional context in comments.

10 Comments

I mean it makes no sense to do this in the first place, but might it be better to return the results of the function f from the call function as well?
@user1330974 they mean actually return the results, currently, call will always return None
Added a return, but of course there's still all kinds of issues with generators, asynchronous functions, etc. It's really not something you should do, unless you have a very specific use for it. @user1330974 the 'clean' way of doing things is to follow convention - in Python, that means clearly calling a function when you need it, with parentheses. Cleaner doesn't always mean fewer symbols.
@user1330974 again, calling the script with code like that is a massive anti-pattern and there's very little use for it that could not be achieved in safer and more efficient ways. You'd do well to learn to write the language as it was intended, instead of trying to subvert it in unreadable and unpredictable spaghetti code.
@user1330974 You could use eval to execute functions like that, however allowing the user to execute arbitrary python is a big security risk, so it should not be done if anyone other than you is using the script. docs.python.org/3/library/functions.html#eval
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.