1

Say I have some arguments passed to a function, I use those arguments to do some calculations, and then pass the results to another function, where they are further used. How would I go about passing the results back to the first function and skipping to a point such that data is not sent back to the second function to avoid getting stuck in a loop.

The two functions are in two different python scripts.

The way I'm currently doing it is by adding any new arguments supposed to be coming from the second script as non keyword arguments, and passing all the arguments from the first function to the second even if they are not needed in the second. The second function passes all the arguments back to the first, and an if condition on the non keyword argument to check whether it has its default value is used to determine if the data has been sent back by the second function. In f1.py:

 def calc1(a, b, c, d = []):
     a = a+b
     c = a*c
     import f2
     f2.calc2(a, b, c)

     If d != []: # This checks whether data has been sent by the second argument, in which case d will not have its default value
         print(b, d) # This should print the results from f2, so 'b' should 
                     # retain its value from calc1.
     return

In the another script (f2.py)

 def calc2(a, b, c):
     d = a + c

     import f1
     f1.calc1(a, b, c, d) # So even though 'b' wasn't used it is there in 
                          # f2 to be sent back to calc1 
     return

1 Answer 1

1

Having two methods recursively call each other is usually a bad idea. It's especially bad between two files. It looks like you want to call calc1(), have it call calc2() internally, and then make a decision about what to do based on the result of calc2().

Is this what you are trying to do?

#### f1.py
import f2

def calc1(a, b, c):
     a = a+b
     c = a*c
     d = f2.calc2(a, b, c)
     # This checks whether data has been sent by the second argument,
     # in which case d will not have its default value
     if d:
         # This should print the results from f2, so 'b' should retain
         # its value from calc1.
         print(b, d)

#### f2.py
def calc2(a, b, c):
    return a + c
Sign up to request clarification or add additional context in comments.

1 Comment

Don't need import f1 in f2.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.