3

If yes, can you suggest a better way than below? Please elaborate/justify.

class X:
    ...

if __name__ == '__main__':
    # TODO: Should we bother doing this?
    errorMessage = 'This class is not meant to have a main method. Do not execute directly.'
    print(errorMessage)
    raise Error(errorMessage)
Hamish Grubijan
  • 9,812
  • 18
  • 89
  • 143
  • if they are not-callable why do you call them? – joaquin Dec 05 '10 at 23:51
  • 1
    Where would that `Error` go? That code isn't in a function and has a `if __name__ == '__main__'` guard, therefore there's no other code calling this (in a `try` black), which in turn means any exception you raise there never gets caught but goes straight to `stderr` instead... result: User gets confusingly-worded error message, then a short confusing stacktrace, then the same error message again. –  Dec 06 '10 at 00:02

4 Answers4

3

A common pattern is to place unit tests for a Python module inside the module body. For example:

if __name__ == "__main__":
    run_unit_tests()

However, if you're not doing this, I wouldn't even bother adding any module body code. If executed as a script, a Python module with no body code will do nothing.

Greg Hewgill
  • 828,234
  • 170
  • 1,097
  • 1,237
3

I would not do this.

Here's why: python -i scriptname.py can be used to load in the script and drop to the interactive console post running of the script. So what you then get is all the defined functions, which you can then manipulate / test as you see fit.

Your way, you'd get an error doing this.

  • 1
    +1 for giving a practical reason, apart from the many ways to say "not needed anyway". –  Dec 05 '10 at 23:58
  • So, what happens if I happen to try to follow Greg's advice and start the unit tests from there? – Hamish Grubijan Dec 06 '10 at 00:34
  • 2
    @Hamish Grubijan: In that case, the tests will run when the interpreter starts and before the interactive `>>>` prompt is shown. If your tests are fast, that's not much of a problem. – Greg Hewgill Dec 06 '10 at 03:29
  • And if they're slow you could always create a unittest function and run that when the interpreter loads or if --test is passed as an argument. That way you can a) change a function b) test just that function then c) make sure all your tests run i.e. you haven't knocked anything else out. It's so annoying going back into code and commenting out bits and/or building a command line parser to run various single unit tests. This way, you escape that. –  Dec 09 '10 at 16:33
2

No, not worth it. After all, if there are no top level function calls, the code won't do anything,

Tyler Eaves
  • 11,379
  • 1
  • 30
  • 39
1

1) No. If something "can't be called", it will raise an exception anyway.

2) The if __name__ == '__main__' construct refers to the module, not the class. "Calling" a class creates an instance of the class. You don't "call" modules, either; you either import or run them.

Please be more careful with your terminology. When you understand the concepts properly, the answers to these sorts of questions become obvious.

Karl Knechtel
  • 51,161
  • 7
  • 77
  • 117