Minor bug in tempfile module (possibly __doc__ error)

James T. Dennis jadestar at idiom.com
Thu May 10 13:30:49 EDT 2007


Marc Christiansen <usenet at solar-empire.de> wrote:
> James T. Dennis <jadestar at idiom.com> scribis:
>> 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):
> If I change this line:
>>                return "foo-%s" % arg
> to:
>                 return "%s-%s" % (tst, arg)

>>        # bar.py
>>        import foo
>>        foo.tst = "bar"
>>        print foo.getTst("testing")

>>        foo-testing     <<<----- NOT "bar-testing"
> Then "python bar.py" prints "bar-testing".

>    0:tolot at jupiter:/tmp> cat foo.py
>    tst = "foo"
>    def getTst(arg):
>            return "%s-%s" % (tst,arg)
>    0:tolot at jupiter:/tmp> cat bar.py
>    import foo
>    foo.tst = "bar"
>    print foo.getTst("testing")
>    0:tolot at jupiter:/tmp> python bar.py 
>    bar-testing

> And regarding the tempfile.template problem, this looks like a bug.
> Because all functions in tempfile taking a prefix argument use "def
> function(... , prefix=template, ...)", only the value of template at
> import time matters. 

> Adia?, Marc

 I suppose my real sample code was def getTst(arg=tst):
 
 Oddly I've never come across that (the fact that defaulted arguments are
 evaluated during function definition) in my own coding and I guess there
 are two reasons for that: I try to avoid global variables and I usually
 use defaulted variables of the form:

    def (foo=None):
        if foo is None:
            foo = self.default_foo

 


-- 
Jim Dennis,
Starshine: Signed, Sealed, Delivered




More information about the Python-list mailing list