4 ms·
It's a common pattern as others have said, but this specific version of it is poor practice. The code should be in a function instead of polluting the global na
by FreakLegion 5y ago
It's a common pattern as others have said, but this specific version of it is poor practice. The code should be in a function instead of polluting the global namespace (otherwise 'console', 'table', and 'color' end up being shadowed if they're used elsewhere in the file). Keep it clean with e.g.:
def main() -> None:
pass
if __name__ == "__main__":
main()
- willm 5y agoThat is not the case. The code within that block will not be in the namespace if that file is imported. Contrary to your example which will have the main function in the module namespace.
- FreakLegion 5y ago> "if that file is imported" Static analysis doesn't care. The script can be executed directly, so it has to be treated as if it will be. The fact that the code behaves differently between prod and dev use is a smell, not a selling point. I'm saying this in the context of writing modern Python with modern tools. In that context it's a poor practice.