0

Using Python 2.7.

Saw that when importing the module "this", an output to the screen (interpreter) is automatically generated.

import this

It outputs some kind of text labeled as "The Zen of Python, by Tim Peters."

I am curious as how that is done. How does one have a module output, or do anything, immediately when it is imported?

[EDIT] This not a duplicate, as the referenced question is on the "ROT13" encoding being used in the module "this", and not as to how to invoke an action from an imported module.

Raw_Input
  • 341
  • 1
  • 5
  • 12
  • 1
    Simply add some print statements. https://hg.python.org/cpython/file/94d8524086bd/Lib/this.py – Ashwini Chaudhary Jan 28 '15 at 00:16
  • The accepted answer is misleading, import in Python is not a copy-paste like `#include` in C: https://docs.python.org/3/reference/import.html#the-import-system – Ashwini Chaudhary Jan 28 '15 at 01:00
  • Use comments if you have an issue with the closing of the question, don't add those things to question body. – Ashwini Chaudhary Jan 28 '15 at 01:16
  • @ Ashwini Chaudhary The duplicate note says to "edit this question to explain how it is different." Best of all would have been if @mhawke had read the question he assumed was a duplicate of mine, instead of killing my question with a negligent mark of "duplicate." – Raw_Input Jan 28 '15 at 01:28
  • @Raw_Input: an answer was already accepted before being marked a dup., so your question was not "killed" by being marked so. You asked how the text was output... the answer is in the source code for the `this` module, and that is shown in the duplicate question. – mhawke Jan 28 '15 at 01:35

2 Answers2

4

when you import something, you are doing the equivalent of copying and pasting a class onto the top of your file. inside that package, anything can be done that can be done in your normal python file, hence a print statement.

Avi Mosseri
  • 1,258
  • 1
  • 18
  • 35
  • 2
    And you're often more concerned in *not* doing something when the module is imported, hence the conditional `if __name__ == '__main__':` – Roberto Jan 28 '15 at 00:24
  • Good info. I am glad @Roberto added that thing about __main__. I would have gone crazy trying to get it to work, as all of my modules have __main__ . – Raw_Input Jan 28 '15 at 00:32
  • 1
    Import doesn't work like that in Python, it is not equivalent to copy-pasting the source code. [When we import something](https://docs.python.org/3/reference/import.html) a module object is created that requires processing the whole module from top to bottom, so if any prints are present at the global level then they are executed as well. If it was equivalent to copy-pasting then we will always end up with all variables from the imported module in our current module. – Ashwini Chaudhary Jan 28 '15 at 00:58
  • correct me if I am wrong, but I believe that it is as i specified, pasting a self contained class on top of the file (of course it is a little more complicated than that) – Avi Mosseri Jan 28 '15 at 01:00
  • Not it's not equivalent to that, for example if that was the case then `global` statements in imported module will then pick the items from current module as well. – Ashwini Chaudhary Jan 28 '15 at 01:13
1

Try this you'll get the hang of it:

main.py

from help import *

help.py

print "I'm a helper script."
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70