I want to combine strings returned by multiple functions conditionally; somehow it's not working:
file1.py
CALL_FUNCTION = {
‘function1’: function1
}
def function1():
return “hello”
def function2():
return “world”
file2.py
from file1 import CALL_FUNCTION
def final_function(condition):
final_string=CALL_FUNCTION[‘function1’] #Calls function1 from file1.py
if(condition)
from file1 import function2
final_string += function2()
return final_string
final_function(true) #This does not work; no error. Expected output is “hello world” when condition is true
Your code had lots of minor bugs. I suspect they all happened because you copied the code by manually retyping it, please use the copy-paste function in future problems.
Here is the corrected version of your code:
file1.py
def function1():
return "hello"
def function2():
return "world"
CALL_FUNCTION = {
'function1': function1
}
file2.py
from file1 import CALL_FUNCTION
def final_function(condition):
final_string=CALL_FUNCTION['function1']() #Calls function1 from file1.py
if condition:
from file1 import function2
final_string += function2()
return final_string
final_function(True) #This does not work; no error. Expected output is “hello world” when condition is true
But after that, everything is working. The functions get called, as expected, the function returns 'helloworld', as expected, and nothing gets printed, as expected.
final_function(True)
would only print something in interactive mode, in batch mode you need to add print() to actually see the result.
print(final_function(True))
Output:
helloworld
If you want to return output of different functions as per "condition", you may import both functions and run each in if-else clause. for example:
In file1.py
def func1():
return "hello"
In file2.py
def func2():
return "world"
Then in separate file3.py
from file1 import func1
from file2 import func2
def func3(cond):
out = func1()
if cond:
out+=' '+func2()
return out
if __name__ =='__main__':
print(func3(True))
Output :
hello world
I'm surprised you're saying that there's no error when you're running this code as there's a couple bugs in your code, so I'm suspecting that this isn't the exact code that you've been running. First of all, make sure to define CALL_FUNCTION
below function1
. Secondly, there's a couple of syntax errors - use True
rather than true
as well as propper string quotes ( '
or "
).
But I believe that the main issue here is that you're never actually calling function1
and you're trying to concat a function object final_string
to a string which is a result of calling function2
. In order to fix that you'd need to replace:
final_string=CALL_FUNCTION['function1']
with
final_string=CALL_FUNCTION['function1']()
This ensures you call function1
and save its result to final_string
instead of saving the function object.
PS: I recommend going over PEP 8 style guide in order to make your code cleaner and easier to read for others in the future.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.