UnboundLocalError: Local variable 'Tee' referenced before assignment

Fredrik Lundh fredrik at effbot.org
Thu Dec 7 06:58:19 CET 2000


jschmitt at vmlabs.com wrote:
> I'm getting an error message that I don't understand.  I'm using this
> handy class that someone kindly on comp.lang.python:
>
> class Tee:
>     """
>     Think of the unix command-line utility
>     """
>     def __init__( self, *fileobjects ):
>         self.fileobjects = fileobjects
>
>     def write( self, string ):
>         for fileobject in self.fileobjects:
>             fileobject.write( string )
>
> and I'm using it kind of like this:
> reportfile = open( os.path.join( workingdir, "report.txt" ), "w" )
> sys.stdout = Tee( reportfile, sys.stdout )    # THIS LINE IS THE PROBLEM
> # do some print's here...
> del Tee
> reportfile.close()

assuming the last code is inside a function:

> del Tee # THIS LINE IS THE PROBLEM

why do you think you have to remove the Tee class
from the local scope?

when you do this, Python thinks Tee is a local variable, which
means that you cannot use it *before* it has been assigned
inside the function.

just remove the "del" statement, and it'll work as expected.

> UnboundLocalError: Local variable 'Tee' referenced before assignment
>
> Does anyone have a spare clue?

did you look in the manual?

</F>





More information about the Python-list mailing list