In <[EMAIL PROTECTED]>, James T. Dennis wrote:

>  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()
>         >>> tf.name
>       '/tmp/mytest-XXXXXX'
> 
>  It doesn't.

The source says:

__all__ = [
    "NamedTemporaryFile", "TemporaryFile", # high level safe interfaces
    "mkstemp", "mkdtemp",                  # low level safe interfaces
    "mktemp",                              # deprecated unsafe interface
    "TMP_MAX", "gettempprefix",            # constants
    "tempdir", "gettempdir"
   ]

Maybe the doc should be clearer in saying "constants" too.

>  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!

You can change it by simply assigning to the name:

In [15]: tempfile.template = 'spam'

In [16]: tempfile.template
Out[16]: 'spam'

If you want to change the outcome of the functions and objects then simply
give the prefix as argument.

In [21]: tempfile.mktemp(prefix='eggs')
Out[21]: '/tmp/eggsBqiqZD'

In [22]: a = tempfile.NamedTemporaryFile(prefix='eric')

In [23]: a.name
Out[23]: '/tmp/ericHcns14'

>  ... 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?

You *can* change it, but it is not used by the code in that module.

Ciao,
        Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to