1

I don't understand how "from _ import _" seems to be behaving...

Somehow, when I run the following line:

from athletelist import AthleteList   

It ALSO executes (in my IDLE Terminal) the print statements at the end of my athletelist file that aren't even a part of the AthleteList class.

Why, when I write "FROM athletelist IMPORT AthleteList", does this statement also run the print() commands that aren't even a part of the AthleteList class?

Mark Puchala II
  • 634
  • 2
  • 8
  • 25
  • 1
    Related: http://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it Notice that `from ... import var` still executes the whole module, it just "forgets" all the other variables except `var` – Markus Meskanen Oct 17 '15 at 16:36
  • I'm not sure I understand... Are you saying that when I type "from _ import _", python is made to execute everything in "from" by default? – Mark Puchala II Oct 17 '15 at 16:41
  • Yes, the interpreter has to read in the entire file, how could it be any other way? – dursk Oct 17 '15 at 16:41
  • @MarkPuchalaII Yes, that's correct. `from x import y` runs all the code in module `x` but only exposes `y` into the current namespace. So basically you are importing only `y` from `x`, but to do that Python has to run the whole file. See [`if __name__ == '__main__':`](http://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it) to fix this issue in your own code. – Markus Meskanen Oct 17 '15 at 16:45

0 Answers0