Strange error, possibly related to var

asked 2012-04-04 23:16:44 +0200

chaesloc2 gravatar image

updated 2012-04-04 23:21:11 +0200

class CTest(object):
    def __init__(self,arg=None):
        var('x,y')
        if type(arg) is list:
            print [x in ZZ for x in arg]
        elif arg in PolynomialRing(ZZ,2,(x,y)):
            pass
a=CTest()

Traceback (most recent call last):
  ...
  File "", line 6, in __init__
UnboundLocalError: local variable 'x' referenced before assignment

Replacing var('x,y') with x,y=var('x,y') eliminates the error. So does replacing PolynomialRing(ZZ,2,(x,y)) with [1,2,3]. Or changing the x in [x in ZZ for x in arg] to z.

edit retag flag offensive close merge delete

Comments

1

I believe this is Python local versus global variable thing. `var('x y')` injects `x` and `y` into the "global namespace", but Python considers all variables talked about inside a function to be "local", even if they have the same names. (This is good or bad, depending on whom you ask.) But I couldn't isolate the exact problem either, frustrating. Maybe this will help anyway?

kcrisman gravatar imagekcrisman ( 2012-04-05 12:56:19 +0200 )edit