diff options
author | Georg Brandl <georg@python.org> | 2008-01-20 11:22:21 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2008-01-20 11:22:21 (GMT) |
commit | 78b11870a4c441e42cf60dca3a6d22f5896311ba (patch) | |
tree | 20d454f372a32cc7b7baf47d8600f14dec0338bc /Doc/howto | |
parent | c31b0f78e5e0d3eaf872a6187fc76f8d63fa5944 (diff) | |
download | cpython-78b11870a4c441e42cf60dca3a6d22f5896311ba.zip cpython-78b11870a4c441e42cf60dca3a6d22f5896311ba.tar.gz cpython-78b11870a4c441e42cf60dca3a6d22f5896311ba.tar.bz2 |
Fix now-wrong :keyword: markup. Remove the section about
"exec without namespace" from the "don't" howto since exec()
can't overwrite names in the calling namespace anymore.
Diffstat (limited to 'Doc/howto')
-rw-r--r-- | Doc/howto/doanddont.rst | 33 |
1 files changed, 0 insertions, 33 deletions
diff --git a/Doc/howto/doanddont.rst b/Doc/howto/doanddont.rst index 0e6b3e8..9e8a052 100644 --- a/Doc/howto/doanddont.rst +++ b/Doc/howto/doanddont.rst @@ -75,39 +75,6 @@ There are situations in which ``from module import *`` is just fine: * When the module advertises itself as ``from import *`` safe. -Unadorned :keyword:`exec` and friends -------------------------------------- - -The word "unadorned" refers to the use without an explicit dictionary, in which -case those constructs evaluate code in the *current* environment. This is -dangerous for the same reasons ``from import *`` is dangerous --- it might step -over variables you are counting on and mess up things for the rest of your code. -Simply do not do that. - -Bad examples:: - - >>> for name in sys.argv[1:]: - >>> exec "%s=1" % name - >>> def func(s, **kw): - >>> for var, val in kw.items(): - >>> exec "s.%s=val" % var # invalid! - >>> exec(open("handler.py").read()) - >>> handle() - -Good examples:: - - >>> d = {} - >>> for name in sys.argv[1:]: - >>> d[name] = 1 - >>> def func(s, **kw): - >>> for var, val in kw.items(): - >>> setattr(s, var, val) - >>> d={} - >>> exec(open("handle.py").read(), d, d) - >>> handle = d['handle'] - >>> handle() - - from module import name1, name2 ------------------------------- |