# The sum function doesn't work in my Jupyter notebook. I get the following error message. Any help appreciated.

Traceback (most recent call last) Cell In[106], line 2 1 number=Integer(20240801) ----> 2 print(sum(number.digits()))

TypeError: 'sage.rings.rational.Rational' object is not callable

edit retag close merge delete

Does the error persist when you run only the following code in a new Jupyter session? (with no other prior code parts, just the following two lines.)

number=Integer(20240801)
print(sum(number.digits()))

( 2024-08-01 07:43:16 +0200 )edit

WorksForMe(TM) in 10.5.beta0 :

sage: number=Integer(20240801)
sage: sum(number.digits())
17


What happen(s|ned) in the 105 previous cells ?

HTH,

( 2024-08-01 11:09:43 +0200 )edit

Sort by ยป oldest newest most voted

You may have introduced variable sum with a rational value somewhere earlier in your code. If that is the case, calling sum() will raise an error like the one you observe.

more

Indeed, one should avoid using names of builtin functions for variables, to avoid losing access to the functions. To get the function back one can do from sage.misc.functional import symbolic_sum as sum

( 2024-08-02 12:06:49 +0200 )edit

@rburing: I believe the default value of sum is the Python's one, not Sage's symbolic_sum.

( 2024-08-02 14:07:58 +0200 )edit
1

The default value is symbolic_sum, which has a fallback to Python's sum when few arguments are given. See e.g. the output of sum?? in a SageMath session.

( 2024-08-02 15:49:55 +0200 )edit

Yes, importing the symbolic_sum as sum did the job. Thank you rburing and Max Alekseyev for helping me out. Much appreciated.

( 2024-08-02 16:57:34 +0200 )edit
2

@voodooguru: a better solution is to not overwrite the value of sum and use another name for the variable.

( 2024-08-02 17:08:59 +0200 )edit