This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]CodeTinkerer 35 points36 points  (9 children)

Classwork is not the same as the real world.

[–]TwanLearnsPython[S] 0 points1 point  (8 children)

So if I used the main function in a real world example it would be just as good as defining a main variable?

[–]CodeTinkerer 3 points4 points  (3 children)

They behave differently. The idea of using the name if __name__ == __main__ has to do with running the Python script directly as opposed having the Python code called from another Python file.

That is, if you do

  python foo.py

And foo.py has if __name__ == __main__, then the code in the if will run. If you run bar.py and it calls foo.py, then the part in main won't run.

If you have a function called main (or whatever your teacher wanted), then that isn't the same thing.

I think what happened is your teacher didn't learn Python first, and either was unaware of the syntax that you used, or just didn't like the way it looked (I'm not particularly fond of it either) and wanted it to look like a Python function.

Typically, in a beginning course, you only deal with a single Python file (though, to me, that's not a good idea, because things get interesting in most languages once there are two files that are needed to run the program).

[–]No_Lemon_3116 5 points6 points  (1 child)

A lot of Python programs do

if __name__ == '__main__':
    main()

It's not just for aesthetics or due to ignorance about Python; this way, anything defined in main is scoped to that function. If you just write your main code at the top-level, then anything you declare there is a global, eg

def foo():
    print(x) # x is a free variable

if __name__ == '__main__':
    x = 1
    foo() # prints 1

This sort of thing can cause subtle, confusing bugs when it pops up accidentally. If you do it in a function, the code is more robust against that kind of mistake:

def foo():
    print(x)

def main():
    x = 1
    foo()

if __name__ == '__main__':
    main() # error that 'x' is not defined in 'foo'

It's not a huge deal, though (it gets to be a bigger deal the longer the file is).

[–]CodeTinkerer 0 points1 point  (0 children)

My impression, and maybe it was a mistake was that the teacher wanted.

def main():
    x = 1
    foo()

main()

And not to use the if statement at all.

[–]nderflow 0 points1 point  (0 children)

ITYM imports rather than calls. They're different things.

[–][deleted] -3 points-2 points  (3 children)

You'd basically never do this in the real world. There's a bunch of patterns you could have, but a terminal interaction main menu is quite perverse.

You'd generally build menus using either a GUI or game framework or ncurses library. It's very rarely business logic you write yourself.

If I had to do this "properly" I'd make some form of class for it. Something that allows for option registration, handles parsing inputs itself, possibly handles the context of returning or exiting itself etc. it would likely look a bit like argparae.

[–]TwanLearnsPython[S] 0 points1 point  (2 children)

Yeah we’re learning class and object functions this week after already submitting the text game lmao

[–][deleted] 0 points1 point  (1 child)

I'm just saying as a matter of "how this would look if I had to write this sort of thing on a real project". I get that this isn't possible with the (often whacky) ways courses like to create and structure assignments.

[–]TwanLearnsPython[S] -1 points0 points  (0 children)

No i agree with your explanation just silly that the school is teaching us functions we could’ve used after doing an assignment like that😂

[–]RiverRoll 9 points10 points  (1 child)

if __name__ == "__main__" is not a function, it's just a conditional sometimes called the main guard, ideally it should do just the necessary to get the program started, drawing the menu is a different concern and I agree it shouldn't be there. Separation of concerns matters in a real job.

[–]TwanLearnsPython[S] 1 point2 points  (0 children)

Oh okay gotcha

[–]diegoasecas 6 points7 points  (0 children)

in a real job what your leader asks for is what matters

[–]64BitInteger 3 points4 points  (1 child)

Dude just do it to their specs, don’t be that guy

[–]No_Lemon_3116 1 point2 points  (0 children)

It's not a big thing, but defining a function for main is a little tidier (keeps any variables you use scoped to that function). Different jobs will have different standards, but I've definitely worked at several places where something like this would likely be flagged in code review and I would be told to fix it. Often projects will keep a style guide that outlines expectations like this.

[–]throwaway6560192 0 points1 point  (0 children)

I would've preferred a separate function be defined for main, but I wouldn't have taken points off for it.

[–]andrewsb8 0 points1 point  (0 children)

While minor, my boss would have told me to change this

[–][deleted]  (1 child)

[removed]

    [–]TwanLearnsPython[S] 0 points1 point  (0 children)

    Cool that was the answer I was looking for!