J
James T. Dennis
Tonight I discovered something odd in the __doc__ for tempfile
as shipped with Python 2.4.4 and 2.5: it says:
This module also provides some data items to the user:
TMP_MAX - maximum number of names that will be tried before
giving up.
template - the default prefix for all temporary names.
You may change this to control the default prefix.
... which would lead one to think that the following code would work:
>>> import tempfile
>>> tempfile.template = 'mytest'
>>> tf = tempfile.NamedTemporaryFile() '/tmp/mytest-XXXXXX'
It doesn't.
In fact I realized, after reading through tempfile.py in /usr/lib/...
that the following also doesn't "work" like I'd expect:
# foo.py
tst = "foo"
def getTst(arg):
return "foo-%s" % arg
# bar.py
import foo
foo.tst = "bar"
print foo.getTst("testing")
foo-testing <<<----- NOT "bar-testing"
Now I would feel like a real idiot if I'd come across that in the
foo/bar case here ... because I clearly don't understand quite *why*
I can't "monkey patch" this value. I would ... but I don't.
First, I wouldn't have written code like this foo/bar stuff; except
to test my hypothesis about why changes to tempfile.template don't
actually affect the values seen by functions in the tempfile namespace.
Secondly, the author(s) of the tempfile module apparently didn't
understand this either. And no one else even noticed that the __doc__
is wrong (or at least misleading -- since the only way I can see to
change tempfile.template is to edit the .py file!
So, I don't feel like an idiot. But I am curious ...
... why can't I change that value in that other namespace? Is it
a closure? (Or like a closure?) Where is this particular aspect
of the import/namespace semantics documented?
as shipped with Python 2.4.4 and 2.5: it says:
This module also provides some data items to the user:
TMP_MAX - maximum number of names that will be tried before
giving up.
template - the default prefix for all temporary names.
You may change this to control the default prefix.
... which would lead one to think that the following code would work:
>>> import tempfile
>>> tempfile.template = 'mytest'
>>> tf = tempfile.NamedTemporaryFile() '/tmp/mytest-XXXXXX'
It doesn't.
In fact I realized, after reading through tempfile.py in /usr/lib/...
that the following also doesn't "work" like I'd expect:
# foo.py
tst = "foo"
def getTst(arg):
return "foo-%s" % arg
# bar.py
import foo
foo.tst = "bar"
print foo.getTst("testing")
foo-testing <<<----- NOT "bar-testing"
Now I would feel like a real idiot if I'd come across that in the
foo/bar case here ... because I clearly don't understand quite *why*
I can't "monkey patch" this value. I would ... but I don't.
First, I wouldn't have written code like this foo/bar stuff; except
to test my hypothesis about why changes to tempfile.template don't
actually affect the values seen by functions in the tempfile namespace.
Secondly, the author(s) of the tempfile module apparently didn't
understand this either. And no one else even noticed that the __doc__
is wrong (or at least misleading -- since the only way I can see to
change tempfile.template is to edit the .py file!
So, I don't feel like an idiot. But I am curious ...
... why can't I change that value in that other namespace? Is it
a closure? (Or like a closure?) Where is this particular aspect
of the import/namespace semantics documented?