None

Steve Holden sholden at holdenweb.com
Wed Oct 24 12:06:10 EDT 2001


"Larry Whitley" <ldw at us.ibm.com> wrote in message
news:9r6lep$tdg$1 at news.rchland.ibm.com...
> Here's the traceback requested:
>
> C:\Projects\Pecos\Traces>analyze3
> u:\trc45\pecos\ c:\projects\pecos\traces\ ('crusader091101.zip',
> 'crusader091101
> .dat', 'crusader091101.a3.txt')
> Traceback (most recent call last):
>   File "C:\Projects\Pecos\Traces\analyze3.py", line 372, in ?
>     main()
>   File "C:\Projects\Pecos\Traces\analyze3.py", line 365, in main
>     process( localPath + files[1], localPath + files[2] ) # process the
file
>   File "C:\Projects\Pecos\Traces\analyze3.py", line 336, in process
>     pc.runningReport( None, riostream ) # to stdout
> UnboundLocalError: local variable 'None' referenced before assignment
>
AHA!!!!!

This is your problem. The interpreter is telling you in this message that it
regards the name None as being that of a variable local to your function or
method. The ONLY reason it is making this assumption is because it has seen
an assignment to None somewhere in the function/method (in this case the
"process()" function).

> Here's the relevant section of code in the neighborhood of the error:
>             if pc.endCond != "":
>                 pc.summarizeData()
>                 riostream = pc.rio()
>                 pc.runningReport( fout, riostream ) # to file
>                 pc.runningReport( None, riostream ) # to stdout
>
At this point the assignment to None has not taken place. But this code is
inside a function that assigns to None, so None is being treated as a local
variable.

> The method runningReport() looks like this:
>     def runningReport(self, fout, riostream):
>         print >>fout, "%8d" % self.startCycn, "%8d" % self.endCycn, "%6d"
%
> (self.endCycn - self.startCycn), \
>               "%8s" % self.command, "%10x" % self.startAddr, "%10x" %
> self.endAddr, "%4d" % self.nBytes, \
>               "%8s" % self.endCond, riostream
>
> My idea was to setup the method to print to file and by substituting None
> for the file object, let the same method send the output to standard out.
>
> Thanks for the help,

You'll get there. See this:

>>> def fOK(a):
...  print "arg is", a, "None is", None
...
>>> def fBAD(a):
...  print "arg is", a, "None is", None
...  None = "something, anything (except None!)"
...
>>> fOK(42)
arg is 42 None is None
>>> fBAD(42)
arg is 42 None isTraceback (innermost last):
  File "<interactive input>", line 1, in ?
  File "<interactive input>", line 2, in fBAD
UnboundLocalError: Local variable 'None' referenced before assignment
>>>

Hope this helps.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list