0

The following code works, however, I also want the currently commented line "perform(action1)" to work -- which would work if I comment-toggle line 3-4. This in turns, would stop the last line from working.

def perform(f):
    a = "A"
    #f()
    f(a)

def action1():
    print("No arg")

def action2(a):
        print(a)

#perform(action1)
perform(lambda a: action2(a=a))

I want function "perform" to receive functions as parameters that have a varying amount of parameters whose values are only assigned within "perform" and not known before.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Why would you want to "perform" a function that has no arguments with two arguments? – Tomerikoo Jan 17 '21 at 15:56
  • Does the code work? – Peter Wood Jan 17 '21 at 15:56
  • By the way, `lambda: action1() == action1` just as `lambda a,b: action2(a=a,b=b) == action2` – Tomerikoo Jan 17 '21 at 15:57
  • You'd be better off trying to have a standardized interface, where all functions can be treated the same. I don't think you'll get a very clean way of deciding if a function accepts arguments or not; which you'd have to check to know if it's safe to pass arguments. I think you'd need to rely on a `try` for that, and that would likely require multiple `try`s; one for each possible argument set. – Carcigenicate Jan 17 '21 at 15:58
  • 1
    You’re going to have to give the correct arguments to `perform()` so wouldn’t it just be simpler all round to simply call `f()` with those arguments, and bion you get argument/parameter checking for free, i.e. normal function usage. – DisappointedByUnaccountableMod Jan 17 '21 at 16:00
  • 2
    This seems like a [XY-problem](https://meta.stackexchange.com/a/66378/291222) – Niko Föhr Jan 17 '21 at 16:17
  • I edited the question so it can hopefully be re-opened. @PeterWood – pixelated_icecream Jan 17 '21 at 16:18
  • Is it @np8? I want a function that accepts functions with a different amount of parameters. That's my X. One could argue that my Y is a different code architecture then? Still, so X is technically not possible in Python? – pixelated_icecream Jan 17 '21 at 16:29
  • Thanks @barny. To give context besides this mwe, parameter a is not known before as it changes within function perform. So that's why I came up with this design. So I guess I'll write some wrappers and stuff. Thanks. – pixelated_icecream Jan 17 '21 at 16:34
  • It's very easy to pass different amount of parameters using [*args and **kwargs](https://realpython.com/python-kwargs-and-args/). If that does not solve your problem, please write a toy example on what you are trying to achieve (with shown inputs and outputs), i.e. a description of the problem at higher level. – Niko Föhr Jan 17 '21 at 16:34
  • @Carcigenicate Thanks I'll try that. You are right, working with `try` s might work but doesn't seem like a good idea. – pixelated_icecream Jan 17 '21 at 16:36
  • Does this answer your question? [How can I find the number of arguments of a Python function?](https://stackoverflow.com/questions/847936/how-can-i-find-the-number-of-arguments-of-a-python-function) – Tomerikoo Jan 17 '21 at 16:58
  • 1
    @pixelated_icecream Why do you want *"a function that accepts functions with a different amount of parameters"*, when `perform` doesn't know how to prepare the parameters correctly? Why is that a requirement? That's your `Y`. What is `X`? – Peter Wood Jan 17 '21 at 18:10

0 Answers0