FAQ
Hi,

I've read various portions of the Python 2.5 documentation in an
attempt to figure out exactly what the following condition represents:

if __name__ == "__main__":
main()

However, I was not able to determine what it is actually checking for.
Could someone point me in the way of a tutorial or explain this for
me? Thanks.

Search Discussions

  • Massimo Di Pierro at Oct 19, 2007 at 11:33 pm
    if the .py file is imported as a module that condition is false else
    (if the .py file is executed) that condition is true
    On Oct 19, 2007, at 6:29 PM, Robert Dailey wrote:

    Hi,

    I've read various portions of the Python 2.5 documentation in an
    attempt to figure out exactly what the following condition represents:

    if __name__ == "__main__":
    main()

    However, I was not able to determine what it is actually checking for.
    Could someone point me in the way of a tutorial or explain this for
    me? Thanks.
    --
    http://mail.python.org/mailman/listinfo/python-list
  • Gabriel Genellina at Oct 20, 2007 at 12:22 am
    En Fri, 19 Oct 2007 20:29:03 -0300, Robert Dailey <rcdailey at gmail.com>
    escribi?:
    I've read various portions of the Python 2.5 documentation in an
    attempt to figure out exactly what the following condition represents:

    if __name__ == "__main__":
    main()

    However, I was not able to determine what it is actually checking for.
    Could someone point me in the way of a tutorial or explain this for
    me? Thanks.
    You won't find it (easily) on the 2.5 docs, but it's covered in the
    upcoming 2.6 tutorial:
    http://docs.python.org/dev/tutorial/modules.html#executing-modules-as-scripts

    --
    Gabriel Genellina
  • Matimus at Oct 20, 2007 at 12:26 am

    I've read various portions of the Python 2.5 documentation in an
    attempt to figure out exactly what the following condition represents:

    if __name__ == "__main__":
    main()

    However, I was not able to determine what it is actually checking for.
    Could someone point me in the way of a tutorial or explain this for
    me? Thanks.
    __name__ is an attribute on a module that shows the standard name for
    that module:
    import sys
    sys.__name__
    'sys'

    The name stays constant even if you do something funny during import:
    import os as something_else
    something_else.__name__
    'os'

    When using the interpreter or running a python script code will be
    executed in a standard module called "__main__".
    __name__
    '__main__'

    In fact, you can even import __main__ if you want:
    import __main__
    __main__.__name__
    '__main__'
    a = 100
    __main__.a
    100

    The common pattern:

    if __name__ == "__main__":
    # do stuff

    IMHO better written:

    if "__main__" == __name__:
    # do stuff

    Allows a module to selectively run code only if it is being run as a
    program. That code will not run if it is imported as a module, because
    in that condition __name__ will return the name of the file (sans .py)
    that the code is in. I've never tried naming a file __main__.py and
    importing it, my guess is that you shouldn't do that :).

    Matt
  • Gabriel Genellina at Oct 20, 2007 at 2:25 am

    En Fri, 19 Oct 2007 21:26:22 -0300, Matimus <mccredie at gmail.com> escribi?:

    The common pattern:

    if __name__ == "__main__":
    # do stuff

    IMHO better written:

    if "__main__" == __name__:
    # do stuff
    I'm intrigued why do you feel the second alternative is better.
    Which is your native language? In English (and Spanish, and many others
    but still not in the majority) the usual ordering is "subject-verb-object"
    or SVO, which matches the first alternative: "If the name is __main__, do
    this..."
    As all the languages I know (not so many!) are SVO, I can't think of any
    equivalent of the second form [that I could say it's better than the first]

    --
    Gabriel Genellina
  • Matt McCredie at Oct 20, 2007 at 5:45 am

    En Fri, 19 Oct 2007 21:26:22 -0300, Matimus <mccredie at gmail.com> escribi?:
    The common pattern:

    if __name__ == "__main__":
    # do stuff

    IMHO better written:

    if "__main__" == __name__:
    # do stuff
    I'm intrigued why do you feel the second alternative is better.
    Which is your native language? In English (and Spanish, and many others
    but still not in the majority) the usual ordering is "subject-verb-object"
    or SVO, which matches the first alternative: "If the name is __main__, do
    this..."
    As all the languages I know (not so many!) are SVO, I can't think of any
    equivalent of the second form [that I could say it's better than the first]
    English is my native language (and only, I'm American :|). The reason
    I like the second version better is simply that:

    variable == literal

    can easily be mis-written as

    variable = literal

    I suppose that isn't a huge issue in Python, since most of the time
    comparisons happen within if and for statements. Even if it is within
    a functions parameters it will raise a SyntaxError exception. There is
    still the case where one might write something like this:

    a = b == 'c'

    or, as I prefer:

    a = 'c' == b

    It is just habit from writing so much C code that way. In C the
    reasoning is that if you have mistyped it, you will catch the issue at
    compile time instead of runtime (which is potentially much more
    difficult to debug). I'm used to seeing that pattern. In terms of
    natural language I do agree with you. It really is just my _humble_
    opinion. I can't make a huge argument for it in Python. To be honest,
    I didn't give it much thought before I wrote it. I am simply used to
    doing it that way, and being irked whenever I see it written the other
    way in C or C++ (and perhaps unjustifiably Python).

    Matt
  • Duncan Booth at Oct 20, 2007 at 1:19 pm

    "Matt McCredie" wrote:

    or, as I prefer:

    a = 'c' == b

    It is just habit from writing so much C code that way. In C the
    reasoning is that if you have mistyped it, you will catch the issue at
    compile time instead of runtime (which is potentially much more
    difficult to debug). I'm used to seeing that pattern. In terms of
    natural language I do agree with you. It really is just my _humble_
    opinion. I can't make a huge argument for it in Python. To be honest,
    I didn't give it much thought before I wrote it. I am simply used to
    doing it that way, and being irked whenever I see it written the other
    way in C or C++ (and perhaps unjustifiably Python).
    Alternatively you could just tell your C compiler to regard dodgy looking
    assignments as an error and stop worrying about it. Much less hassle all
    round.

    e.g. For gcc -Werror -Wparentheses, for Microsoft compiler -W4 -WX, or an
    appropriate #pragma in a header file for each.
  • Jeff at Oct 20, 2007 at 3:40 am
    A common pattern is to put test code in the block there, too, for
    modules.

    Re comparison ordering, perhaps it's as in PHP, where string literals
    should always go before a variable in a comparison in case evaluating
    the variable causes an error :)

    Mas, ese orden nunca uso yo ;).
    On Oct 19, 10:25 pm, "Gabriel Genellina" wrote:
    En Fri, 19 Oct 2007 21:26:22 -0300, Matimus <mccre... at gmail.com> escribi?:
    The common pattern:
    if __name__ == "__main__":
    # do stuff
    IMHO better written:
    if "__main__" == __name__:
    # do stuff
    I'm intrigued why do you feel the second alternative is better.
    Which is your native language? In English (and Spanish, and many others
    but still not in the majority) the usual ordering is "subject-verb-object"
    or SVO, which matches the first alternative: "If the name is __main__, do
    this..."
    As all the languages I know (not so many!) are SVO, I can't think of any
    equivalent of the second form [that I could say it's better than the first]

    --
    Gabriel Genellina
  • Steven D'Aprano at Oct 20, 2007 at 4:27 am

    On Sat, 20 Oct 2007 00:26:22 +0000, Matimus wrote:

    The common pattern:

    if __name__ == "__main__":
    # do stuff

    IMHO better written:

    if "__main__" == __name__:
    # do stuff
    Apart from looking weird, what's the difference?


    --
    Steven.
  • Peter Otten at Oct 20, 2007 at 7:32 am

    Steven D'Aprano wrote:
    On Sat, 20 Oct 2007 00:26:22 +0000, Matimus wrote:

    The common pattern:

    if __name__ == "__main__":
    # do stuff

    IMHO better written:

    if "__main__" == __name__:
    # do stuff
    Apart from looking weird, what's the difference?
    In C this style is sometimes propagated as a means to avoid accidental
    assignment:

    if (main="main") {...} /* valid C, always true */
    if ("main"=main) {...} /* syntax error */

    In Python it would be cargo cult.

    Peter

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedOct 19, '07 at 11:29p
activeOct 20, '07 at 1:19p
posts10
users8
websitepython.org

People

Translate

site design / logo © 2022 Grokbase