Ask Your Question

Revision history [back]

Are you writing these lines in a Python file?

The error message seems to indicate that (1..n) was parsed as trying to apply the method .n to the float 1., which is similar to other errors one can get when using certain Sage syntax shorthands involving a dot (.) in a Python file.

Besides (1..n) and [1..n], I am thinking of syntax goodies to choose names for generators of an object as you define this object, such as in these examples:

R.<x,y,z> = PolynomialRing(QQ)
K.<a> = NumberField(x^2 - x - 1)
F.<a,b> = FreeGroup(2)

If used in a Python file, these will result in an error, because Python doesn't like these uses of the dot. In a Python file the examples above would become:

R = PolynomialRing(QQ,['x','y','z'])
x,y,z = R.gens()

P = PolynomialRing(QQ,'x')
x = P.gen()
K = NumberField(x**2 - x - 1)
a = K.gen()

F = FreeGroup(2)
a,b = F.gens()

(with appropriate import statements at the start of the file).

EDIT (knowing that this is happening in an IPython notebook):

If you put the line

%load_ext sage

in a cell by itself and evaluate it, and then put the rest in a second cell,

def f(n):
    for k in (1..n):
        print n

f(5)

then your example will work.

Note that, with everything in the same cell, as you had, if after getting the AttributeError you re-evaluate the cell, then you get this warning:

The sage extension is already loaded. To reload it, use:
  %reload_ext sage

but your function works and you get the expected output

5
5
5
5
5

The explanation is that %load_ext sage needs to be evaluated first before the preparser is activated letting you use any Sage syntax that violates standard Python syntax rules.

I would even put the definition of f and the command f(5) in different cells, so that you can work on the function definition separately from applying it to different values:

  • while working on the definition of f (getting rid of any syntax errors), no use in trying to apply it.

  • once f is defined, to try it with different arguments, no need to define it again each time.


Original answer:

Are you writing these lines in a Python file?

The error message seems to indicate that (1..n) was parsed as trying to apply the method .n to the float 1., which is similar to other errors one can get when using certain Sage syntax shorthands involving a dot (.) in a Python file.

Besides (1..n) and [1..n], I am thinking of syntax goodies to choose names for generators of an object as you define this object, such as in these examples:

R.<x,y,z> = PolynomialRing(QQ)
K.<a> = NumberField(x^2 - x - 1)
F.<a,b> = FreeGroup(2)

If used in a Python file, these will result in an error, because Python doesn't like these uses of the dot. In a Python file the examples above would become:

R = PolynomialRing(QQ,['x','y','z'])
x,y,z = R.gens()

P = PolynomialRing(QQ,'x')
x = P.gen()
K = NumberField(x**2 - x - 1)
a = K.gen()

F = FreeGroup(2)
a,b = F.gens()

(with appropriate import statements at the start of the file).