0

I have the following data frame

import pandas as pd
d = [['a','1'],['b','2']]
d = pd.DataFrame(d,columns=['col1','col2'])

I want to create new functions with names from column 'col1' and copy the body from below into the new created function.

def func1():
    print('Hi') ...... more lines of code

The end functions should be like this

 def a():
        print('Hi') ...... more lines of code
 def b():
        print('Hi') ...... more lines of code
Rohan Bali
  • 157
  • 6
  • 2
    Why do you want to create multiple functions with same body? Functions exist to avoid code repetition (among other features). – Michael Butscher Jul 29 '21 at 11:26
  • https://www.kiite.com/python/answers/how-to-run-multiple-functions-at-the-same-time-in-python. I am trying to use threading and testing concurrent processing.. however the same function is not been passed in two different multithreading jobs. So I need multiple functions to try this out – Rohan Bali Jul 29 '21 at 11:32
  • If you need a deep copy of a function, maybe this could be relevant? https://stackoverflow.com/questions/13503079/how-to-create-a-copy-of-a-python-function – skywalker Jul 29 '21 at 11:40
  • 2
    The link doesn't work on my phone. You can run the same function in multiple threads at the same time. Each function **invocation** holds its own argument values and local variables, therefore two or more invocations of the same function can run side by side. – Michael Butscher Jul 29 '21 at 11:41
  • 2
    I still don't completely understand your question, I think you tried to strip down the question to get rid of irrelevant things (which is good) but you accidentally removed useful context (that you are doing it because of some multithreading issues). See XY problem (https://en.wikipedia.org/wiki/XY_problem). – skywalker Jul 29 '21 at 11:43
  • 1
    @MichaelButscher I think he meant this link: https://www.kite.com/python/answers/how-to-run-multiple-functions-at-the-same-time-in-python – skywalker Jul 29 '21 at 11:48
  • 1
    I don't think you need to copy the function at all. You can just reuse it and pass it to `threading.Thread(target=func).start()` multiple times. – skywalker Jul 29 '21 at 11:51
  • @les I have used this... I have different parameters that are to be passed. If I am using threading.Thread(target=func(a)).start() and threading.Thread(target=func(b)).start(). These are not running in parallel. Its a series execution then – Rohan Bali Jul 29 '21 at 11:56
  • Yes, that is because the function is called when **constructing** the Thread object. You need to pass *a function*, not a *result of the function* (which you pass, because you call the function). To create a new function on the same line, you can use lambda, like this: `threading.Thread(target=lambda: func(a)).start()` – skywalker Jul 29 '21 at 11:58
  • With () after a function you invoke it and use the return value as argument for "target" but you want the function itself (function object) as argument. Regarding the different arguments there is the "functools" module with the function "partial" to create functions with preset arguments dynamically. – Michael Butscher Jul 29 '21 at 12:01

0 Answers0