Trouble with variable "leakage"?
John Machin
sjmachin at lexicon.net
Thu Mar 20 23:51:39 EDT 2008
On Mar 21, 2:05 pm, 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.
Actually xlist[i] and ylist[i] are explicably the same for all
relevant values of i.
> I have no idea why. Please help me.
Keep reading ...
>
> 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)
Aside: I am NOT going to try to guess what your algorithm should do; I
just hope you know what the above is doing ... like why not del
relist[i] if you're not interested in the popped value?
> 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)
insert here:
print [id(e) for e in xlist], [id(e) for e in ylist]
You'll see that list(xlist) gives you a shallow copy i.e. it doesn't
dig deeper and copy the contents of the lists at the next level. You
need the deepcopy function of the copy module -- do read the docs.
Aside #2: "print" is a statement, not a function.
> print(xlist)
> print(ylist)
>
[snip]
HTH,
John
More information about the Python-list
mailing list