Tuesday, 6 August 2013

Python: A variable which is assigned and not changed changes along with another

Python: A variable which is assigned and not changed changes along with
another

Working in Sage which is basically python, I believe. I gave the following
code.
def lfsr_1(regs,tabs):
I=regs
leng=len(I)
count=0
while True:
FB=0
print "Before"
print I
print regs
print temp
for i in range(0,leng):
FB=FB+tabs[i]*I[i]
for i in range(0,leng):
regs[leng-(i+1)]=regs[leng-(i+1)-1]
I[0]=FB
count=count+1
print "After"
print I
print regs
print temp
if (I==regs):
break
The input variables were in initialized as follows
tabs=[GF(2)(1),0,0,1,1,1]
regs=[GF(2)(0),1,1,0,1,1]
temp=regs
However, Im getting an output as:
Before
[0, 0, 1, 1, 0, 1]
[0, 0, 1, 1, 0, 1]
[0, 0, 1, 1, 0, 1]
After
[0, 0, 0, 1, 1, 0]
[0, 0, 0, 1, 1, 0]
[0, 0, 0, 1, 1, 0]
Dont know how this happens as 'I' changes along with 'regs'. 'I' is never
changed in code. Is there something wrong with my assignment?

No comments:

Post a Comment