[Numpy-discussion] local variable referenced before assignment

Vincent Schut schut at sarvision.nl
Tue May 31 05:34:19 EDT 2011


On 05/31/2011 11:04 AM, Edoardo Pasca wrote:
> Dear all,
>
> sometimes I encounter the problem that calling many times a function
> it happens that some local variables are not defined and the procedure
> crashes.
>
> For example I have a function defined as
>
> def procedure(tt, Ctissue, WeinmannFit, bodyWeight):
>      for maxFastSampling in range(1,len(tt)):
>          if tt[maxFastSampling] - tt[maxFastSampling-1]>  10:
>              break
>
>      if numpy.equal(Ctissue[:maxFastSampling] , 0 ).all():
>          # do something
>
> Now the inputs are all numpy.array except bodyWeight that is a number.
>
> The program calls the function procedure many times but at some point
> fails at the evaluation of numpy.equal with
>
> UnboundLocalError: local variable 'maxFastSampling' referenced before assignment
>
> I wonder whether you see any way that the maxFastSampling variable
> might not be set.

Edo,

When len(tt) is <2, range(1, len(tt)) gives an emtpy list, the for loop 
will never execute and maxFastSampling will not be set.

VS.
>
> Python 2.6.4
> numpy 1.5.0
>
> Thanks
>
> Edo
>




More information about the NumPy-Discussion mailing list