Instead we ask the question "is my module the entry point to this #Python process?"
We do that with this very strange looking condition:
if __name__ == "__main__":
main()
Why does this work? ๐ค
Well, each module has a __name__ variable in it which represents the name of that module. There's also __file__, which is its file path and __doc__ which is its docstring.
But when Python runs a module as a script, it does something different...
Every #Python process has one module which acts as its entry point.
When you run "python3 my_script.py", you're telling Python to use the my_script.py file as the entry point module.
The entry point module doesn't get its own name, but a special one instead: "__main__".
There are 3 methods I implement on most #Python classes I make.
โข __init__: the initializer method
โข __repr__: the programmer-readable string representation
โข __eq__: implement a nice sense of equality (what the == operator does)
__init__ & __repr__ always, __eq__ usually
If you're inheriting from another class you'll likely get some (or all) of those for free.
But if you're creating your own class, it's easy to overlook __repr__ and even easier to overlook __eq__.
But both of those methods can make it easier to work with your class instances.
The tricky one of those 3 methods is __eq__.
Not all classes really have a sensible notion of equality outside of the "equality is identity" behavior that Python provides out of the box.
And implementing __eq__ makes objects unhashable by default, though that's rarely an issue.