0

Let's say I have a good reason to have these 2 separate modules:

first_module.py

import second_module

def say_hello(user, text):
   print(f"{text} -> {user}")

if __name__ == "__main__":
   second_module.run(say_hello)

second_module.py

def run(func):
   # here I want to modify the func function by removing the first parameter
   # as stated above, func in this example is the say_hello from first_module.py
   # I want to remove the user parameter and let only the text parameter
   # is this even possible?

I tried to use lambda, something like this:

modified_method = lambda args, kw: func(*args[1:], **kw)

However, this didn't work

My expected result is to manipulate (modify or create a new version) the func function inside the run function. The reason is that the second_module.py has no idea about the function say_hello at 'compile time'. Basically, second_module.py is a library that will be used later by first_module.py, so the run function inside second_module.py needs to be generic

basilisk
  • 1,156
  • 1
  • 14
  • 34
  • What do you mean by "removing the first parameter"? What is your expected output? – blarg Apr 08 '22 at 23:13
  • Not questioning the two separate modules bit, but is there a reason you would need to first import `say_hello()` from `first_module.py`, and *then* redefine it? If it actually uses both parameters, you'll have to modify the body of it, anyway, so you don't get a `NameError`. The only reason I can think of why it would be important to do it this way is if you are doing `import first_module`, and want to have the modified version still be within the `first_module` namespace. – baileythegreen Apr 08 '22 at 23:14
  • What value do you expect `user` and `text` to be when called by `run()`? – blueteeth Apr 08 '22 at 23:16
  • You could probably achieve this with a wrapper, but I don't know if that would address your specific needs. – baileythegreen Apr 08 '22 at 23:20
  • I edited the question and provided more input – basilisk Apr 08 '22 at 23:27
  • @baileythegreen how exactly? A wrapper will not work since the run function does not about the say_hello function at "compile time". The run function needs to be generic and work with arbitrary functions. Can you provide an example of what you mean by "wrapper" – basilisk Apr 08 '22 at 23:30
  • "However, this didn't work" Yes, because if you want the `lambda` to accept a variable amount of positional and keyword args (so that you can use `*args` and `**kw` to call the helper), then you need to write it so that it does so: `lambda *args, **kw: ...`. Please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and https://meta.stackoverflow.com/questions/261592. If you are having difficulty with a `lambda`, write it as an ordinary function first, and then debug it normally: e.g. by checking what the parameter values are at the start, etc. – Karl Knechtel Apr 08 '22 at 23:39
  • I'm not sure about something that would do what you describe in the edits you made to the question. I'm not going to be able to attempt an example right now, though. – baileythegreen Apr 08 '22 at 23:39
  • I linked a "duplicate" that gives a thorough overview of the general process for this kind of function manipulation. But maybe this was just a typo? – Karl Knechtel Apr 08 '22 at 23:40
  • how would a link to decorator basics help me? The question is not a duplicate, the link you added has nothing to do with my question – basilisk Apr 09 '22 at 10:21

0 Answers0