3

I created the code below, but when I click on the click me button I get the following error message:

TypeError: 'mpfr' object is not callable

Would someone know what is wrong with the code?

import gmpy2 as g
from ipywidgets import widgets
from IPython.display import display

button = widgets.Button(description="Click Me!")
display(button)

max_precision = g.get_max_precision()
pi = g.const_pi()
g.set_context(g.context())

def set_bits_precision(decimal_precision):
    bits_precision = int(decimal_precision/g.log(2))
    if (bits_precision > max_precision): bits_precision = max_precision
    ctx = g.get_context()
    ctx.precision = bits_precision
    return

def square_root(number):
    return g.sqrt(number)

def circle_perimeter(radius):
    return 2*pi*radius 

def on_button_clicked(x):
    return square_root(x)

set_bits_precision(10)
print(pi)
button.on_click(on_button_clicked(2))
Aman Agarwal
  • 623
  • 7
  • 17
Olivier
  • 607
  • 1
  • 6
  • 21
  • Usually 'object is not callable' usually means you have used a left-paren after an identifier. In your case perhaps you have 'mpfr (' when probably you meant something like 'mpfr, (' . That is, you are missing something (like a punctuation) after the mpfr. – Art Swri Jun 20 '18 at 15:55
  • 1
    `button.on_click` takes a callable as its argument. `on_button_clicked(2)` does not evaluate to a callable, it evaluates to a number (of type `mpfr`) – jbch Jun 20 '18 at 15:57
  • @ArtSwri No. That is not the issue at all. Did you even look at their code? – jbch Jun 20 '18 at 15:57

1 Answers1

7

button.on_click must be given a callback function. You pass the result of on_button_clicked evaluated with parameter 2 (so, literally the square root of 2) instead of passing in a function. You can use partial function evaluation to do what you want, by replacing your last line of code with:

import functools
button.on_click(functools.partial(on_button_clicked, 2))
jbch
  • 591
  • 2
  • 6
  • 1
    Interesting! But I'm getting the following error: "TypeError: on_button_clicked() takes 1 positional argument but 2 were given" – Olivier Jun 20 '18 at 18:32
  • @OlivierdeBroqueville on_click passes 1 argument to the callback. I'll edit my answer... – jbch Jun 20 '18 at 18:45
  • Thank you jbch. It's working fine now. I had to do the following changes to the on_button_clicked function >> def on_button_clicked(x, b): – Olivier Jun 20 '18 at 18:49
  • @OlivierdeBroqueville Yeah that will work, sorry I got busy elsewhere, probably wont have time to edit my answer for a while. Feel free to edit it or add your own answer with the correct code. – jbch Jun 20 '18 at 20:21