5

As a part of my school CS class, I am reading Python Programming by John Zelle. In the book, Zelle talks about surrounding the invocation of a main method with

if <condition>:
    main()

where the condition is something like __name__ == '__main__'. My computer science teacher said that the necessity of this loop depends on if the program is importable and run-directly. I know that because there are 2 variables, there can only be four combinations of importable and run-directly.

I know from the book and my CS teacher that

If a program is importable and is run directly, then main must be conditional.

However, I do not know the cases, which is what I am trying to figure out:

  1. If not importable and not run-directly
  2. If importable and not run-directly
  3. If not importable and run-directly

I think, but am not sure, that these are the correct answers.

  1. If not importable and not run-directly, then it must be in a conditional
  2. If importable and not run-directly, then it doesn't have to be in a conditional.
  3. If not importable and run-directly, then it doesn't have to be in a conditional.

"Are the statements above correct?" is what I am trying to figure out.

I have tried finding this information in the book, but is unfortunately unavailable.

  • 3
    This is a well written and researched homework question. A welcome surprise, thank you! – enderland Nov 17 '15 at 3:40
4

You're very close, but are missing one aspect.

If a program is importable and is run directly, then main must be conditional.

If not importable and not run-directly, then main is irrelevant. Actually, the entire contents of the file are irrelevant.

If importable and not run-directly, then main should not exist.

If not importable and run-directly, then main does not have to be in a conditional. It's still a good idea, as it'll help avoid accidentally omitting it were needed, and it causes no harm.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.