Oct-14-2019, 12:14 PM
If you do it this way:
This is what you usually do:
def foo(): if __name__ == '__main__': print('bar') foo()The print statement will not run if the function is imported. The function is called when imported (line 5), but since the print is suppressed, it looks like nothing is happening. But then if you call it from the module that did the import, nothing will happen then either, because the if statement is suppressing the print statement.
This is what you usually do:
def foo(): print(bar) if __name__ == '__main__': foo()Now if you import the file, the function is not called at all, because the last line is suppressed by the if statement. But if the module that does the importing calls the foo function, the print works, because it is no longer being suppressed by the if statement.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures