function - Why does sage treat local python variables as global? -
new sage , python, i'm not sure i'm abusing. i'm trying define following function acts on input list a, every time input function affects global value of a. how can make behave locally?
def listxgcd( ):     g,s,t=xgcd(a.pop(0),a.pop(0))     coeffs=[s,t]     while a!=[]:         g,s,t=xgcd(g,a.pop(0))         coeffs=[s*i in coeffs]         coeffs=coeffs+[t]     return coeffs   i've tried setting b=a , substituting b everywhere doesn't work either don't understand. need declare sort of sage-y variable thing?
def listxgcd( ):     b=a     g,s,t=xgcd(b.pop(0),b.pop(0))     coeffs=[s,t]     while b!=[]:         g,s,t=xgcd(g,b.pop(0))         coeffs=[s*i in coeffs]         coeffs=coeffs+[t]     return coeffs   much thanks!
you passing reference container object listxgcd function , function retrieves elements container using pop. not scope issue, fact there operating directly on container have passed function.
if don't want function modify container, make copy of it:
import copy def listxgcd( ain ):     = copy(ain)     ...   or better, access elements using indexing, if container allows it:
... g,s,t=xgcd(a[0],a[1]) ... in range(2,len(a)):     g,s,t=xgcd(g,a[i])     ...      
Comments
Post a Comment