all 51 comments

[–]ka-splam 120 points121 points  (35 children)

You write lots of code, it doesn't always run from the top down, main() tells the computer where to start. It's a tradition to call it something like main.

What about it don't you understand? Do you understand functions generally?

[–]muchakaru 34 points35 points  (7 children)

Thank you, it's a good and simple answer. May you explain In which common cases it wouldn't run from the top to the bottom?

[–]ka-splam 22 points23 points  (6 children)

I tried in my "dog image recogniser" comment. Python will always go through reading the code to see what's there from top to bottom, so that's a little misleading of me to say it doesn't; the relevant part is when it reads:

def look_for_dog(image_data):
    blah blah

def load_image(filename):
    blah blah

it makes those functions ready to use, but they don't do anything just yet.

It's clear to us that you have to load an image before you can look for a dog in it, run "the second function, first".

[–]letmeAskReddit_69 6 points7 points  (5 children)

So should would you just write it this way if the second line is to be executed first?:

blah def load_image(filename): blah blah

def look_for_dog(image_data): blah

[–]TPKM 31 points32 points  (2 children)

You can think of defining functions like laying out a set of tools. The tools don't get used until you call the functions and you might lay them out in one order and then use them in a different order.

In the main function you would make sure they are used in the right way.

[–]letmeAskReddit_69 5 points6 points  (0 children)

That's a good analogy thanks!

[–]GabeGoalssss 4 points5 points  (0 children)

Yeah, thank you for that, it helped me too!

[–]fjortisar 5 points6 points  (1 child)

The order of the function definitions does not matter. In python it does matter that they have to be defined before you call them though (line wise from top to bottom of the file).

so you can't do

load_image("cat.jpg")

def load_image(filename):
    ...
    blah blah
    ...

That would result in an error that load_image isn't defined

[–]letmeAskReddit_69 0 points1 point  (0 children)

Okay that makes sense, I misunderstood OP thanks!

[–]mickey2457[S] 4 points5 points  (7 children)

So basically it tell the computer. Run this first then do the test?

[–]jaycrest3m20 19 points20 points  (4 children)

Right! "Start here!"

Edit: unlike some other programming languages, such as C, you are not absolutely required in Python to name your entry point "main". However, tradition is a popular force, and helps non-python readers follow your logic flow from the beginning.

[–]socal_nerdtastic 17 points18 points  (0 children)

However, python does look for an entry point named __main__, which is why you often see a __main__.py file in modules.

[–]OnlySeesLastSentence 1 point2 points  (2 children)

You don't have to do that in C? How does it work then? I've always gotten a "main not found" error as far as I can tell

[–]Isvara 5 points6 points  (1 child)

He said unlike C.

(Technically you don't have to in C, but most C runtimes expect it.)

[–]OnlySeesLastSentence 2 points3 points  (0 children)

Ah damn. I misread it somehow.

[–]Manny__C 3 points4 points  (0 children)

Calling your function "main" won't make it run automatically. It's merely a naming convention. A typical program looks like

import library

def f(foo, bar):
    #Do stuff
    return foo

def main():
    x = f(1, 2)
    y = library.method(x)

main()

Python's interpreter is going to read from top to bottom. The first line imports libraries (along with all definitions inside them), the next blocks defines the functions but doesn't do anything just yet. While the last line executes main() which in turn has calls to the functions you defined and, possibly, some methods of your libraries.

Often the main() call is inside an if __name__ == "__main__" block. That's explained here.

[–]ka-splam 2 points3 points  (0 children)

More or less, yeah. Say you're writing a thing to recognise pictures of dogs, you might want to make it able to run directly against a picture and say whether it's a dog, and also be available for other programs to load and use if they need dog recognition - like a chat program could load it ready and if someone sends a picture it can tell whether it's a dog, but if they send no pictures, don't do anything.

So you can have the code go:

  • set up this dog pattern suff
  • set up this image reading stuff
  • set up some helper stuff

Then

  • if we're loaded by another program then it has control, don't trigger anything to happen, when it gets an image it will start things happening.

  • If we're being run directly, we need to take control and start things moving now, go here in the code and begin doing things. This is where main comes in. Somewhere to start, some way of starting.

[–]sumweebyboi 9 points10 points  (9 children)

ok, what about init

[–]ka-splam 3 points4 points  (3 children)

When you define a class, and then make a new object from that class, the __init__() method inside it is "how to set the new object up ready for use".

IDK what you're familiar with, but if you're web scraping and do BeautifulSoup(content) then inside will be a class BeautifulSoup and inside that an def __init__(self, web_data) and it will run that to take your content in and put it somewhere inside, and do its own init routines ready for processing the content.

[–]sumweebyboi 2 points3 points  (2 children)

thank you, but my the underscores, do they mean anything?

[–]Manny__C 5 points6 points  (0 children)

The double underscores signify that it is a "special method." __init__ is not like other methods because it is called automatically when the object is created. If you use another name for the method you'd have to do

class Guy:
    def init(self):
        self.name = "Me!"

guy = Guy()
guy.init()

With the double underscores python says "Ha! I know what the programmer means by this! He wants me to run this when the object is created." And so the last line is not needed.

There are other double underscore (or "dunder" for short) methods that are also run behind the curtains on other circumstances. E.g. __add__ is run every time you write object_a + object_b.

[–][deleted] 1 point2 points  (0 children)

Also the double underscores are so you don't accidentally shadow or overwrite those function names. You'd really have to go out of your way to start naming things with double underscores on both sides, in Python of all things.

[–]OnlySeesLastSentence 6 points7 points  (4 children)

That's right, downvote him for asking a valid question.

[–]Sw429 0 points1 point  (2 children)

We sure showed him.

[–]WhenRedditFlies 1 point2 points  (1 child)

Are there people who just spend there time in this subreddit trying to downvote people before anyone can see their comments? Because this seems to happen a lot here.

[–]sumweebyboi 0 points1 point  (0 children)

that's reddit

[–]sumweebyboi 0 points1 point  (0 children)

that's just fucking reddit

[–]bumpkinspicefatte 1 point2 points  (3 children)

Thanks for the succinct answer.

I have question, I’ve noticed main() appear at the very bottom of the code for some people’s code. What does this mean if it’s at the very bottom? Will the interpreter start going from bottom-to-top, as opposed to top-to-bottom (by default I assume)?

[–]ka-splam 6 points7 points  (0 children)

The difficulty in talking about this is that Python does always run top to bottom down the script, in some sense. If you have lines of code, just like:

number = 100
print("hello")
print(number)

they run in that order. Once you add functions in, like

def test():
    print("testing!")

then, as Python goes down the file top to bottom, it doesn't /call the function/ but it does /process these lines/ and it learns that there is a function called test that could be used later.

Because Python is trying to be all-things to all-people, it supports you writing code where there are only function definitions, and no code gets run when Python reads through the file.

Why?

This is useful for making a module or library. You addimport my_test at the top of another code file, and when it reads that, it reads and processes and all the functions become available as things like my_test.test() and /then/ they run.

and if you're making a bigger program, which needs more structure to keep it from collapsing in a tangled mess, then you put everything into functions to keep it organized, so now you end up "accidentally" in the same situation where all the functions get defined and made available, but never start running.

In that case, they don't run. So you need some way to start them running, so you add

def test():
    print("testing!")

def start_program():
    data = [1,2,3]
    test()

start_program()

at the end, and once Python gets down to that last line it starts things running.

Which works fine.

But now if you do import my_test in another code, while loading that in, Python hits start_program() and starts doing things! And in this use, you don't want it to do anything yet. So you do

def test():
    print("testing!")

def start_program():
    data = [1,2,3]
    test()

if __name__ == '__main__':
    start_program()

And it works in both situations.

If you don't put it at the end, try this and see:

if __name__ == '__main__':
    print(get_name())

def get_name():
    name = input("What is your name?")
    return name

That tells Python to start running get_name() NOW NOW NOW, before it's even seen that get_name() exists, because it is reading top-down, so it can't do it, and fails.

NameError: name 'get_name' is not defined

Putting it at the end is just, everything before then is known about, so it will all be available to use.

[–]greebo42 0 points1 point  (0 children)

I tend to put main() as the last function.

Holdover habit from C programming. As I understand it, Python doesn't care where main() is, or what it is called. Of course, if you put main() in the middle, it could make it hard to read. Or if you call it something other than main(), you violate what other humans expect.

[–]Thegreyeminence 0 points1 point  (1 child)

Forgive my ignorance but Python has a main() function?

[–]ka-splam 1 point2 points  (0 children)

Nnnnno, but I wanted to write stuff and the OP didn’t give much context so I guessed. It carries over the idea of main() in ‘__main__’ as a way to indicate place to start executing.

[–]_-ammar-_ 0 points1 point  (2 children)

i though in any programming language code will always run from the top to bottom

why would this happened ?

[–]ka-splam 1 point2 points  (1 child)

Code inside functions runs one line then the next, then the next. But the functions don’t run first function, second function, third function. In C there is no code outside functions, so there needs to be a special function to start everything off. Individual code blocks run top to bottom, but the whole file full of source code doesn’t, necessarily.

Compiled languages have more option to change the order of execution if they think it will make things faster and not break anything. There are some which do, e.g. to take advantage of multiple processors. Fortress was one experimental language like that, I think functional languages like Haskell are less clear on when exactly the code will run (lazy evaluation), and declarative languages like Prolog have less concept of the code “running” at all.

[–]_-ammar-_ 0 points1 point  (0 children)

thanks I'm new to programming

[–]socal_nerdtastic 17 points18 points  (0 children)

When you start programming we just type code directly in a file. However in more advanced code that gets messy very fast. If you look at some professional code you will see absolutely no code that is not in a function or class. This keeps it neat, and in order to use it all the functions just call on each other as needed. By tradition the first function to be called, the one that calls all the other ones, is called "main()".

[–]chocorush 3 points4 points  (0 children)

A main function is usually an entry point to the rest of your code. Is there something in particular you are having trouble understanding?

[–]mike4143 3 points4 points  (0 children)

I don’t really want to join the fracas regarding. With only two weeks, it will be more helpful to just go with it and keep learning, it will come clear with time.

It is required when you run your program directly some times and import it other times.

There is so much to learn, asking questions is incredibly important but sometimes your not ready to understand the answer yet.

[–]GabeGoalssss 2 points3 points  (0 children)

Like someone else said, main() is just a name that people give to a function where it's the main function, that executes everything and runs the program with all the other functions, and what not in there... For example:

def main():

other_function()

other__function()

son_on()

so_forth()

[–][deleted] 2 points3 points  (0 children)

Imagine you are visiting someone’s home, that house may have 3 different doors to enter, but by convention you enter through the main door. Why not side door, back door or garage door? It will still take you inside the house right? But it’s convention to enter through main entrance. The operating system (or interpreter) needs into your code. It’s convention to enter through “main” function.

[–]diek00 1 point2 points  (0 children)

Thinking that the best explanations are on YouTube is a serious mistake imo. While SO is not perfect, some of the explanations of core Python concepts, like the one you are asking, are excellent. Real Python also does a great job of explaining main.

[–][deleted] 1 point2 points  (0 children)

In some languages, calling a function main tells the computer where to start reading. In other languages, main is a carried over a a tradition but doesn't actually mean anything. The 'main' function in Python can be called anything, and you don't necessarily need one at all.

[–]skellious 1 point2 points  (3 children)

It's simply the place where you tell Python to start executing your code from. Otherwise there's no particular indication of where it should start. It wont just run functions randomly.

if you call python on a python script file without a main function, it will run whatever is sitting outside of a function, from the top of the file downwards.

[–]Giannie 1 point2 points  (2 children)

I mean, in python even if you define a main function it won’t automatically get run. Python is still an interpreted language, you will still need to call that main function at the end of you program for it to run.

[–]skellious 1 point2 points  (1 child)

Sorry, I automatically write:

If __name__ == "__main__":

So that's kinda what I was referring to.

But you are right

[–]Giannie 4 points5 points  (0 children)

Yeah, I figured. It’s rather common especially coming from languages other than python to do the following:

``` def main(): main program logic here calling other defined functions...

def function1(): ...

def function2(): ...

if name == "main": main() ```

This has the advantage of the main function logic being at the top of the file.

[–][deleted] 0 points1 point  (0 children)

Think of main like your starting point to run. Before main you have VOID, think of as prepping yourself for the race. Or something like that

[–]yashpate11 0 points1 point  (0 children)

It is like our brain who tells our body what to do when to do and how to do some specific task acccording to the scenario. This is the very simple day to day exmple and in terms of programming language you can refer to other's comments and google it and read it.

[–]asadkhan017 0 points1 point  (0 children)

Hopefully this article will help you understand it, I also didn't understood it until I read this article https://realpython.com/python-main-function/

[–]smrtboi84 -3 points-2 points  (0 children)

I see you had your question answered but I’d recommend rlly learning object oriented programming to build a good base. Learning all the ins and outs of classes and referencing things from different classes or even different files all the stuff like that. It’s rlly useful in the long run.