UnboundLocalError: Local variable 'Tee' referenced before assignment

jschmitt at vmlabs.com jschmitt at vmlabs.com
Thu Dec 7 15:38:46 EST 2000


The reason I wanted to invoke 'del' is because I wanted to close the
file and closing the file will cause the instance of Tee to write to a
closed file and I don't have the save sys.stdout right there.
Obviously this code needs more work than this.  And looking at it now,
it's obvious to me that I can't simply kill that instance without first
restoring sys.stdout.  Duh.

As for RTFM, I didn't specifically look in the manual for this, because
I didn't associate 'del' with the error message that I saw.  As I said,
I didn't understand the error message at all.

Thanks for the info.  This newsgrous is awesome and the people
answering questions here rock!!

John

In article <%FFX5.2390$O%.371404 at newsc.telia.net>,
  "Fredrik Lundh" <fredrik at effbot.org> wrote:
> 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>
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list