Pregunta

If I'm on Python 2.7 and I've just started using the Spyder IDE.

On a terminal python version if I do

any(i ==1 for i in [1,2,3,4])

I get the answer

True

While if I do the same in Spyder I get the response

<generator object <genexpr> at 0x3fc8af0>

Why is it doing that? Am I missing a setting or might this be a different version of Python (it says 2.7)

¿Fue útil?

Solución

Here's a quote from another related question about Spyder's Python console behavior:

One of Spyder's primary design goals is to make interactive scientific computing as painless as possible. To facilitate that, by default Spyder launches a custom-tailored interactive Python session at startup. It achieves this customization by setting an environment variable called PYTHONSTARTUP which specifies the path to a script that will be executed at interpreter startup. You can control this setting under Preferences...Console...Advanced settings. By default, Spyder points to scientific_startup.py, which imports a whole host of scientific modules and functions directly into the main namespace so that quick, interactive exploration is easy.

As a consequence, the behavior you are experiencing is because you are actually calling the numpy versions of any and all which have been placed directly into the main namespace. To verify this, call

np.any(i ==1 for i in [1,2,3,4])

or

np.all(i ==1 for i in [1,2,3,4])

in the Spyder Python console, and you'll get the same generator objects being returned. By the way, these last two calls magically work because the startup script also does import numpy as np. For more details on what else is imported, type scientific at the Spyder Python console prompt.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top