Reputation: 97
The idea is this - say I have a function in module2.py
called def run(example):
which has a data-frame object as the result of running it, let's call it df_1
. If I call module2.run(example)
inside module1.py
will I be able to continue to use the result, df_1
, inside module1.py
and manipulate the data the way I want? Would I need to put the function in a class inside module2.py
to do this? Or how should I structure my code if this was the plan?
Upvotes: 1
Views: 47
Reputation: 294508
When module2.run(example)
was called, it created an object that occupies space in ram and returned a pointer to that object.
# Inside module2.py
import pandas as pd
def run(example):
return pd.DataFrame(1, range(10), range(10))
# Inside module1.py
import module2
df_1 = module2.run(example)
df_1.loc[:] = 2
print(df_1)
0 1 2 3 4 5 6 7 8 9
0 2 2 2 2 2 2 2 2 2 2
1 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2
3 2 2 2 2 2 2 2 2 2 2
4 2 2 2 2 2 2 2 2 2 2
5 2 2 2 2 2 2 2 2 2 2
6 2 2 2 2 2 2 2 2 2 2
7 2 2 2 2 2 2 2 2 2 2
8 2 2 2 2 2 2 2 2 2 2
9 2 2 2 2 2 2 2 2 2 2
Upvotes: 1
Reputation: 426
Yes, you can do exactly the way you have said, in module1.py, you just have to import module2.py like "import module2" or if the function is inside a class, you can import it like "from module2 import classname" Then you can create the object of the class and then call the method. You need to return the dataframe object from run function in order to use it. You may keep it global too, but recommended way of doing things.
for example if you have class
from module2 import MyClass
my_obj1 = MyClass()
df1 = my_obj1.run(param1,param2 etc)
Upvotes: 1