Python Forum

Full Version: __name__ and __main__ in functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here's a short program:
lwords = ["a", "an", "and", "as", "at", "but", "for", "how", "if", "in", "of", "off", "nor", "or", "so", "the", "to", "up", "via", "with", "yet"]

def make_title(sT):
    rlist = []
    rs = ""
    for word in sT.split():
        if not word.isupper() and word not in lwords:
            word = word.title()
        rlist.append(word)
        
    if not rlist[0].isupper():
        rlist[0].title()
    if not rlist[-1].isupper():
        rlist[-1] = rlist[-1].title()
        
    for word in rlist:
        rs += " " + word
        
    rs = rs.strip()
    return rs

def main():
    sT = input("Enter the title: ")
    print(make_title(sT))
          
if __name__ == "__main__": #infinite loop results with L26 and L27 uncommented.
    main()
I can follow the logic until the last two lines. What do __name__ and __main__ do with regard to [this] function[s]?

Actually, I guess main() is a function defined to contain the main function so no mystery there. What is this __name__, though?

Thanks!
You've authored 100 posts and this is unfamiliar to you? Bizarre! Using Jupyter Notebook must really be a completely different experience than writing Python the "normal" way.

Welcome to the wonderful world of dunders. Just like "normal" attributes, dunders come in two flavors: variables and methods. The double underscores surrounding the name indicate that Python things this thing is special. If you've written many classes you are probably familiar with dunder methods like __str__() and __repr__(). Python calls these methods when it wants to print an instance of your class as a pretty string (__str__) or a more informative representation (__repr__). Sometimes called "magic methods", the only magic is that the str() knows to call __str__().

You can read about dunder methods here:

https://www.geeksforgeeks.org/dunder-mag...ds-python/

There are also dunder variables. Like the methods, the double underscores on both ends of these variables mark them as being important to Python. I've never seen them called magic variables, but they are automatically assigned values, as if by magic. It is harder to find a list of dunder variables, but they are sprinkled about in this document.

https://docs.python.org/3/reference/data...ml#modules

Unfortunately the same document talks about dunder methods as well, and there are a log of double underscores. Over 1000!

According to said document:
Quote:__file__
The pathname of the file from which the module was loaded, if it was loaded from a file. The __file__ attribute may be missing for certain types of modules, such as C modules that are statically linked into the interpreter. For extension modules loaded dynamically from a shared library, it’s the pathname of the shared library file.
Strangely enough, there is no mention of the most common use of the __file__ module attribute nor any mention that __file__ is set to "__main__". To learn about that, you need to look here:

https://docs.python.org/3/library/__main__.html
Quote:What is the “top-level code environment”?
__main__ is the name of the environment where top-level code is run. “Top-level code” is the first user-specified Python module that starts running. It’s “top-level” because it imports all other modules that the program needs. Sometimes “top-level code” is called an entry point to the application.

That's the long of it. The short of it is that __file__ is set to "__main__" when the file is the "top-level code environment". Programmers use this to prevent some code from executing when the file is imported (__name__ will not be "__main__"). In your particular example, the programmer does not want to call main() when the module is imported.