Trouble with variable "leakage"?

J

Jeremy N

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)
dlist.pop(0) ; dlist.pop(i)
for j in range(len(relist)) :
d1 = float(nlist[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.append(float(r1))

del d1, d2, dx, relist, dlist

ylist = list(xlist)
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[j]), str(xlist[j]),
str(relist[j][( (i!=0) * ((j>=i)+(i-1)) )]) ) )
d1 = float(xlist[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[j] = float(dx) ; #print(ylist[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)
 
M

MRAB

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)
dlist.pop(0) ; dlist.pop(i)
for j in range(len(relist)) :
d1 = float(nlist[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.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[j]), str(xlist[j]),
str(relist[j][( (i!=0) * ((j>=i)+(i-1)) )]) ) )
d1 = float(xlist[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[j] = float(dx) ; #print(ylist[j])

ylist is the same list as xlist, so changing ylist[j] also
changes xlist[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.
 
J

John Machin

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 and ylist 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 if you're not interested in the popped value?
dlist = list(nlist)
dlist.pop(0) ; dlist.pop(i)
for j in range(len(relist)) :
d1 = float(nlist[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.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top