What is if name == “main_”, and how do I use it.
change_speed = (speed) => [...document.querySelectorAll('video')].map(v => v.playbackRate=v.playbackRate+speed)
When a python module is called it is assigned the __name__ of __main__
otherwise if it’s imported it will be assigned the __name__ of the module.
Concrete example # [1]
Let’s create a module to play with __name__ a bit. We will call this module
nodes.py. It is a module that we may want to run by it’self or import and use
in other modules.
#!python
# nodes.py
if __name__ == "nodes":
import sys
import __main__
print(f"you have imported me {__name__} from {sys.modules['__main__'].__file__}")
if __name__ == "__main__":
print("you are running me as main")
I have set this module up to execute one of two if statements based on whether
the module it’self is being ran or if the module is being imported.
Note it is not common to have a if __name__ == "nodes": block, this is just
for demnonstration purposes.
running python nodes.py # [2]
Running a python script with the...