Skip to content
Advertisement

Calling Function’s return value to a script from another module?

I am new to the advanced problems of python. I am trying to get good if you want to make any correction it is welcomed.

I want to do run function in Run File. I can’t pass the return value of a function without altering its value.

Then assign fpath in the function firstfile.py to the secondfile.py script’s sav_img variable and be able to work inside of another function.

I need to do this beacuse i can only access to the run file. When i update repo.

Run Order

Run File

import Firstfile
i = "C:\Users\Hp\Desktop\1.jpg"
First File.file_path(i)

First File

def file_path(fpath):

      return fpath

Second File

from First File import file_path
sav_img = file_path()
print(sav_img)

Error on sav_img

TypeError: file_path() missing 1 required positional argument: 'fpath'

sav_img = file_path.fpath doesn’t working either. I know i need to pass value but i don’t want to alter its value

Advertisement

Answer

One possibility is to get the function to save the return value when called with a parameter, but return the saved value when called with none.

First File

def file_path(fpath = None):
    if fpath is None:
        return file_path.fpath
    else
        file_path.fpath = fpath
        return fpath

Second File

from FirstFile import file_path
sav_img = file_path()
print(sav_img)

However, this relies on the first caller never using None as the parameter.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement