by cranium · 3y ago · 0 comments
import antigravity
As others have mentioned, every line of code not in a function/class gets executed when imported, except if guarded with an `if __name__ == '__main__':` (only true when executing the script with `python xxx.py`). A related catch: functions' default arguments are also evaluated when imported the first time.Try creating `fun.py`:
def evil():
print("Gotcha")
return 1
def abc(x = evil()):
print(x)
Now: `python fun.py` or `python -c "import fun"`