This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: "import this" is cached in sys.modules
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: barry Nosy List: Arfrever, BreamoreBoy, barry, eric.snow, ezio.melotti, georg.brandl, pitrou, r.david.murray, rhettinger, terry.reedy, tim.peters
Priority: low Keywords:

Created on 2013-11-05 01:55 by rhettinger, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (16)
msg202183 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-11-05 01:55
The "this" module doesn't import functions; instead, it prints directly to stdout.  Accordingly, there is no reason to cache this module in sys.modules.

Ideally, a learner should be able to type "import this" more than once in  a Python session.
msg202184 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2013-11-05 01:59
"Special cases aren't special enough to break the rules." ;-)
msg202191 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2013-11-05 03:26
You could add the following to the bottom of this.py:

import sys
del sys.modules['this']
msg202195 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-11-05 05:56
FWIW, a question about "this" has come up on multiple occasions when I teach Python classes.
msg202198 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2013-11-05 07:01
I'd take their question as an educational opportunity.

reload(this)
msg202199 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2013-11-05 08:11
Whatever we decide to do here, it **must** also be done for antigravity.py.
msg202213 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-11-05 14:42
It seems to me that having import this work more than once would teach a beginner the *wrong* lesson about how python import works.  So I agree that it should be a teaching moment, not a bug to be fixed.
msg202222 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2013-11-05 16:28
If it ain't broke don't fix it.
msg202227 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2013-11-05 17:42
+0 to just doing a reload.  At the point you show someone "import this", it may be premature to be explaining reloading to them.  Python is great because you usually don't have to hand-wave through some concepts in order to explain others. [1]

Also, under Python 3 you have to import reload() separately:

from imp import reload
reload(this)


[1] http://www.boredomandlaziness.org/2011/08/scripting-languages-and-suitable.html
msg202229 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2013-11-05 18:04
IMO the fact that importing happens only once is also a very important one, so much the better this helps in learning it early.

The bad thing is that opening this.py to see what's happening will not really enlighten the beginner :)
msg202453 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-11-09 01:06
The current ugly, implicit, complex ROT13? + custom decoder version of this.py violates the initial tenets that it prints. I suppose that is an intentional joke. If it were explicit and simple:

text = '''<title>
<text lines>
...'''
print(text)

then people could follow up
  import this
with
  this.text
and learn something about accessing module attributes.
msg202489 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-11-09 17:46
Terry is right here. The "this" module doesn't seem to have been thought as an educational tool in the first place.
msg202494 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-11-09 19:19
I think this should be closed as won't fix.

Having "import this" behave differently from any other import is counter-intuitive (students will start asking "why if I reimport 'this' the code gets executed every time but it doesn't work with my module?").

If they want to read the Zen over and over again they can check PEP 20.
msg202511 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-11-10 09:04
> Having "import this" behave differently from any other import is counter-intuitive

I agree. My proposal (by design) does not change the property of executing only when first imported. I merely proposed that the text either start as cleartext or that the decrypted text be saved as a module attribute. *This* would make 'this' *more* like normal modules.

Having side-effect code executed on import, as opposed to when running as main, is unusual. (Idlelib.idle is another intentional example, and one which currently has the same problem.) But I agree that this unusual behavior should remain for both.

Having module code that is intentionally obfuscated and as about as inefficient as possible without being totally ridiculous is, I hope, unique, and not at all like other modules.

Even if Tim wants to keep the literal encrypted, and the rot13 codec is not available in Py3 (?), the decoding would, I believe, be much more efficient using str.translate. Or the text could be encoded and decoded with one of the pairs in the binascii or base64 modules.
msg202545 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2013-11-10 18:41
Reassigned to Barry, since he wrote this module ;-)  FWIW, I wouldn't change it.  It wasn't intended to be educational, but a newbie could learn quite a bit by figuring out how it works.
msg202556 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2013-11-10 19:15
I completely agree with Tim.  The 'this' module was a *joke* and a stealthy one at that.

http://www.wefearchange.org/2010/06/import-this-and-zen-of-python.html

About the only thing I'd support is adding some comments to the code to either explain what's going on a little better, or provide some history to what this module is and why it's there.  Then again, if you want to do that, please be sure to also add some documentation, but you better make it *funny*. :)
History
Date User Action Args
2022-04-11 14:57:53adminsetgithub: 63698
2013-11-16 04:56:17eric.snowlinkissue19621 superseder
2013-11-10 19:21:51gregory.p.smithsetnosy: - gregory.p.smith
2013-11-10 19:15:57barrysetstatus: open -> closed
resolution: wont fix
messages: + msg202556
2013-11-10 18:41:29tim.peterssetassignee: tim.peters -> barry

messages: + msg202545
nosy: + barry
2013-11-10 09:04:44terry.reedysetmessages: + msg202511
2013-11-09 19:19:38ezio.melottisetnosy: + ezio.melotti
messages: + msg202494
2013-11-09 17:46:43pitrousetnosy: + pitrou
messages: + msg202489
2013-11-09 01:06:30terry.reedysetnosy: + terry.reedy
messages: + msg202453
2013-11-05 18:04:27georg.brandlsetmessages: + msg202229
2013-11-05 17:42:53eric.snowsetmessages: + msg202227
2013-11-05 16:28:03BreamoreBoysetnosy: + BreamoreBoy
messages: + msg202222
2013-11-05 14:42:21r.david.murraysetnosy: + r.david.murray
messages: + msg202213
2013-11-05 08:11:37georg.brandlsetnosy: + georg.brandl
messages: + msg202199
2013-11-05 07:23:19Arfreversetnosy: + Arfrever
2013-11-05 07:01:22gregory.p.smithsetnosy: + gregory.p.smith
messages: + msg202198
2013-11-05 05:56:59rhettingersetassignee: tim.peters
messages: + msg202195
2013-11-05 03:26:17eric.snowsetnosy: + eric.snow
messages: + msg202191
2013-11-05 01:59:25tim.peterssetnosy: + tim.peters
messages: + msg202184
2013-11-05 01:55:02rhettingercreate