On Sat, Oct 28, 2017 at 5:05 AM, ROGER GRAYDON CHRISTMAN <d...@psu.edu> wrote: > While teaching my introductory course in Python, I occasionally see > submissions containing the following two program lines, even before > I teach about functions and modules: > > if __name__ = '__main__': > ... main() > > When I ask about it, I hear things like they got these from other instructors, > or from other students who learned it from their instructors, or maybe > from some on-line programming tutorial site. > > I'm all on board with the first of these two lines -- and I teach it myself > as soon as I get to modules. > > My question is more about the second. > > Do "real" Pythonista's actually define a new function main() instead > of putting the unit test right there inside the if? > > Or am I correct in assuming that this main() is just an artifact from > people who have programmed in C, C++, or Java for so long that > they cannot imagine a program without a function named "main"?
If it's JUST for unit tests, I'd expect no main(), but instead to have it go straight into unittest.main(). IMO, the construct you show there implies three things: 1) This module is intended to be run from the command line 2) This module is intended to be imported by other modules 3) If imported by another module, this can also be invoked as if it were the top-level app. If #1 is not true, you don't need any sort of "if name is main" code, because that's not the point. If #2 is not true, you don't need to guard your main routine, because the condition will always be true. (Note that external unit tests count as -- https://mail.python.org/mailman/listinfo/python-list