r/learnpython • • 5d ago

Basically, if main

In Python, when you run your program in another folder, we add this, but I don't understand why. What's the point of doing this? If someone could explain it in a simplified way, I'd appreciate it.

9 Upvotes

27 comments sorted by

View all comments

Show parent comment

1

u/Adrewmc 4d ago edited 4d ago

I’m pointing out that for the most part it’s sort of useless in the grand scheme of things to even use.

I just dont see much use of printing directly to shell and immediately losing the value as too important to programming.

I think you should be aware of the idea, but it really doesn’t have much use case for most programmers, especially people just learning.

However, knowing that things execute when call during the importing process is an importing thing to know.

Frankly I think they should just make built in function.

if is_root():
. #or a better keyword on the spot here
. main()

1

u/codeguru42 4d ago

Who said anything about printing to shell? Sounds like you are talking about something different than the question. Or at least tangential to it

1

u/Adrewmc 3d ago edited 3d ago

I mean https://github.com/python/cpython/blob/main/Lib/uuid.py

That what the command does…print to the shell, you lose it right after unless you copy it by hand…

That’s the example you used.

I merely asking for a use case that you really need to if __name__ == main? Printing to the shell isn’t a good example in my opinion as it basically ends there. I kind of see maybe it a bit easier if you run the same little things a lot, but in that case I would just make a specific entry file for that.

Writing a fast simple script is like what Python is known for.

1

u/codeguru42 3d ago

Like I said, don't get stuck on one example. The if name pattern is extemely useful

1

u/Adrewmc 3d ago edited 3d ago

But I’m saying the use cases are extremely limited (and it best to have a better design) and learn Python yeah…it exists

By the time you get to argparse…you have a firm grasp of the language. And you can do what you want.

It’s mostly a distraction for beginners.

Most beginners, should only be aware of it, but not be using it.

1

u/codeguru42 3d ago

I disagree with your claim that the use cases are limited

1

u/codeguru42 3d ago

If you take the time to read the other replies, the whole point of if __name__ == main is so that you can choose to run a python file as a script or to import it and call its functions directly. This is incredibly useful in many ways.

As you say, just printing to the screen has limited utility. But print() doesn't actually print to the screen. Rather it prints to a stream called stdout. By default stdout goes to the terminal so you see it on the screen. You can change this default in a lot of different ways. For example you can use | to send the output to the input of another program or you can evaluate a program with $() and send it as an argument to another program. It is far from useless.

1

u/codeguru42 4d ago

And just because you don't find the pattern useful doesn't mean no one does. I use it often and so do many other python programmers.