First time here? Check out the FAQ!

Ask Your Question
0

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

asked 0 years ago

voodooguru gravatar image

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

Preview: (hide)

Comments

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()))
tolga gravatar imagetolga ( 0 years ago )

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,

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 0 years ago )

1 Answer

Sort by » oldest newest most voted
2

answered 0 years ago

Max Alekseyev gravatar image

updated 0 years ago

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.

Preview: (hide)
link

Comments

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

rburing gravatar imagerburing ( 0 years ago )

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

Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )
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.

rburing gravatar imagerburing ( 0 years ago )

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

voodooguru gravatar imagevoodooguru ( 0 years ago )
2

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

Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 0 years ago

Seen: 411 times

Last updated: Aug 01 '24