0

I'm a beginner : sorry the answer is found nowhere on the web.

Why people in python typically write :

if __name__ == "__main__":
    main()

instead of (without space) :

if __name__=="__main__":
    main()

Is there a better convention to use ?

  • 5
    It's a convention to make the code easier to read. You should have a look at [PEP 8](https://www.python.org/dev/peps/pep-0008/#whitespace-in-expressions-and-statements) – Christophe Feb 04 '22 at 10:54
  • 1
    Some operators are not easy to distinguish when there's no space between them. There are also operators which are words, which make it impossible to not have spaces on either site of them. Consider e.g. `if condition_a or condition_b` versus `if condition_aorcondition_b`. Because there are cases where you *must* have spaces, and consistency is a key element in making code more readable and maintainable, better add space even when it's not strictly needed. – Some programmer dude Feb 04 '22 at 10:54
  • 1
    It's just a convention, but it's probably a good idea to follow it, so your code is easier to read for other people. If you don't want to think about such things, a code formatter might be a nice solution. Black is the most popular one out there: https://pypi.org/project/black/ – tpwo Feb 04 '22 at 10:59

1 Answers1

1

This is a convention is part of the Python Enhancement Proposals, namely, PEP8.

You can check for mor information in:

yogaxpto
  • 28
  • 4