Hello Hans,
On Fri, Mar 08, 2024 at 02:24:17PM +0000, Hans Ekkehard Plesser wrote:
Dear Python experts among the NEST Users!
I just notice a strange interaction between NEST and Ipython. In a freshly started Ipython, I execute the following three statements
import nest n = nest.Create('parrot_neuron', positions=nest.spatial.free([(x, 0) for x in range(10000)])) n
The last one takes extraordinarily long, over 10 seconds, I think. But if I instead do
print(n)
it executes instantaneously.
What happens is that IPython's pretty printing calls (a bit down the line) the IPythonDisplayFormatter() class. The first thing that does is test if for the given object there is an _ipython_display_() method - if that exists, it calls that, otherwise it falls back to a more generic method. In your case the latter happens, it falls back to the more generic method and that is what then produces the output.
When looking up if there is an _iython_display_() method, the first thing IPython does is check for a method called _ipython_canary_method_should_not_exist_() - and it is precisely this lookup that takes so long. You can verify this by
getattr(n, "_ipython_canary_method_should_not_exist_")
which will also take rather long. As the output here also contains some SLI things, one might need to dig deeper to actually speed this up.
Instead, adding a check specifically for exactly the above mentioned lookup of _ipython_canary_method_should_not_exist_() also does the job (for me), see the attachment. With that workaround, your example returns instantaneously - as one would expect it to.
regards, Harold