Trouble with variable "leakage"?

MRAB google at mrabarnett.plus.com
Thu Mar 20 23:37:12 EDT 2008


On Mar 21, 3:05 am, Jeremy N <Subject... at gmail.com> wrote:
> I am working with Python in Maya, and have run into a problem with a
> variable changing its contents without being scripted to do so. The
> various print() statements below were from my efforts to track down
> where it was occurring. I left them in so that anyone running this
> will more easily see what's happening.
>
> On the line that reads  'dx = d1 / dx ; print("dx = %f" % dx)'  there
> is something happening to the variable that is being printed
> repeatedly between the lines. The print statements prior to this
> particular line print  '...xlist[0][1] = 0.5'  . However, on this
> line, that variable is being updated to reflect a new value, when no
> assignment to that variable has been made at that time.
>
> This leads me to believe that the variables  'dx'  and  'xlist[0][1]'
> are inexplicably linked. I have no idea why. Please help me.
>
> a=[5,0,3,4]
> b=[8,3,0,10]
> c=[2,4,10,0]
>
> nlist = [a,b,c]
> xlist = [[],[],[]]
>
> for i in range(len(nlist)) :
>         relist = list(nlist)
>         relist.pop(i)
>         dlist = list(nlist[i])
>         dlist.pop(0) ; dlist.pop(i)
>         for j in range(len(relist)) :
>                 d1 = float(nlist[i][0])
>                 d2 = float(relist[j][0])
>                 dx = float(dlist[j])
>                 r1 = 1 - ( abs(d1-dx) / float(d2) )
>                 if r1 == 0.0 :
>                         r1 += (d1 < d2)
>                 xlist[i].append(float(r1))
>
> del d1, d2, dx, relist, dlist
>
> ylist = list(xlist)
This is performing only a shallow copy.

xlist refers to a list containing sublists. You're copying the
_references_ to the sublist, not the sublists themselves.

> print(xlist)
> print(ylist)
>
> for i in range(len(xlist)) :
>         relist = list(xlist)
>         relist.pop(i)
>         for j in range(len(relist)) :
>                 print( "!!!!!!!!!!!!!!! NEW LOOP AT ( %d:%d ) !!!!!!!!!!!!!!!" %
> ( i, j ) )
>                 print("%s / (%s + %s)" % ( str(xlist[i][j]), str(xlist[i][j]),
> str(relist[j][( (i!=0) * ((j>=i)+(i-1)) )]) ) )
>                 d1 = float(xlist[i][j]) ; print("d1 = %f" % d1)
>                 print( "...xlist[0][1] = %s" % str(xlist[0][1]) )
>                 d2 = relist[j][( (i!=0) * ((j>=i)+(i-1)) )] ; print("d2 = %f" % d2)
>                 print( "...xlist[0][1] = %s" % str(xlist[0][1]) )
>                 dx = d1 + d2 ; print("dx = %f" % dx)
>                 print( "...xlist[0][1] = %s" % str(xlist[0][1]) )
>                 dx = d1 / dx ; print("dx = %f" % dx)
>                 ylist[i][j] = float(dx) ; #print(ylist[i][j])
ylist[i] is the same list as xlist[i], so changing ylist[i][j] also
changes xlist[i][j].

>                 print( "...xlist[0][1] = %s" % str(xlist[0][1]) )
>                 print( "||| xlist[2][0] = %s" % str(xlist[2][0]) )
>                 print( "...\nxlist = %s\n..." % str(xlist) )
>
> print(xlist)
> print(ylist)
>
HTH.



More information about the Python-list mailing list