3

I have just started learning Python and one thing is bugging me is the exact type of "__main__". So far, I have seen that "__main__" is mostly used as string literal like __name__ == "__main__".
I mean, it is not a function, it is not a variable. With all the other built-in variables and functions starting with __ it is just confusing. Any special reason it has to start with __? I know it has special meaning in the context of modules and namespaces, but I am talking purely in the terms of basic/primitive data types.

Tejas Sarade
  • 1,144
  • 12
  • 17

5 Answers5

4

Yes, it's just a string. The fact that it has two underscores is because it's a "special name". Special names in Python have these two leading and trailing underscores.

What it actually represents is also explained in the docs:

29.4. __main__ — Top-level script environment

'__main__' is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.

A module can discover whether or not it is running in the main scope by checking its own __name__, which allows a common idiom for conditionally executing code in a module when it is run as a script or with python -m but not when it is imported:

if __name__ == "__main__":
     # execute only if run as a script
     main()

For a package, the same effect can be achieved by including a __main__.py module, the contents of which will be executed when the module is run with -m.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
3

Yes, "__main__" is a string literal, since module names are strings. The module is called __main__ to distinguish it from modules named main, and since it is a system-assigned name.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

Yes, it is a string literal. Like magic methods like __init__ start and end with 2 underscores in order not to be mixed with other methods, __main__ has such naming not to be mixed with names of existing files.

Eugene Primako
  • 2,767
  • 9
  • 26
  • 35
  • 1
    That may be nitpicking but `__main__` is in fact an existing module, just try `import __main__` :) – MSeifert Jun 23 '18 at 16:50
  • @MSeifert You are right, thanks :) Removed "modules" from my answer. – Eugene Primako Jun 23 '18 at 16:53
  • Thanks for that tip, after importing `__main__` I can access existing variables using dot notation like `__main__.varname`. That clears a ton of confusion. – Tejas Sarade Jun 23 '18 at 16:58
  • 2
    BTW, here is some discussion on ```import __main__```: https://stackoverflow.com/questions/24023601/is-it-good-practice-to-use-import-main. There are several points on why such imports can be bad. – Eugene Primako Jun 23 '18 at 17:00
1

Yes, it's just a string literal that by design looks a bit like the names of some special methods in Python.

There is a bit more information in the docs: https://docs.python.org/3/library/main.html

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

You are right __main__ is a string literal. __name__ is just a variable (commonally called a dunder alias).

Here are two easy ways to prove this:

Using type():

>>> type(__name__)
<class 'str'>
>>>

This should be enough but you can also overwrite the value of __name__ for example (you shouldn't however):

>>> __name__ = 'hello'
>>> __name__
'hello'
>>>

My answer here expains what it is.

__name__ is a DunderAlias - can be thought of as a global variable (accessible from modules) and works in a similar way to global. It is a string (global as mentioned above) as indicated by type(__name__) (yielding <class 'str'>), and is an inbuilt standard for both Python 3 and Python 2 versions.

Xantium
  • 11,201
  • 10
  • 62
  • 89