3 ms·
You get an hint that Python's import is not a simple "add name to scope" when importing a builtin package opens a web browser: import antigravity As other
by cranium 3y ago
You get an hint that Python's import is not a simple "add name to scope" when importing a builtin package opens a web browser:
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"`
- capableweb 3y agoHow is that different from what most dynamic languages does? I can't think of one where you wouldn't be able to do the same.