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

Question:

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

Asked By: Mindtrip

||

Answers:

The error you are getting is self explanatory: your function file_path() requires one positional argument, named fpath.

You have not provided this argument when you write:

sav_img = file_path()

Hence the error.

If you want to learn more about scripts in different files, take a look at the official module documentation

I’m not quite sure why you define a function whose sole purpose is to return it’s provided argument, I can’t see why you would ever want to do that, outside of getter functions within a class that, but that is a different story.

Answered By: Mouse

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.

Answered By: quamrana
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.