[IPython-User] Python code in a markdown cell

Fernando Perez fperez.net@gmail....
Fri Apr 13 15:31:50 CDT 2012


On Fri, Apr 13, 2012 at 11:56 AM, Michael Hadmack <hadmack@gmail.com> wrote:
> x = 10
> y = 5
> z = x + y
> print z
> z  # alternatively
>
> Is there any way to do this on a single line?

None other than changing your last line to:

z = x+y; z

Mathematica prints them automatically, but that's because either in
the Mathematica language assignment itself returns a value, or because
they special-cased assignment and parse it out.  Matlab has the same
behavior:

>> a  = 4

a =

     4

This is different in Python, and for better or worse, IPython tries to
respect the basic behavior of the language when possible.  And the way
Python works follows its principle of "explicit is better than
implicit": if you want to see your object printed, then ask for it.
Implicit re-printing of assignments could lead to very annoying
behavior if

a = some_huge_expression()

then swamped your screen with pages of output simply because you
wanted to *assign* to a.

So despite the slight occasional inconvenience of Python's choice, I
think it's  a sensible one and IPython will continue to honor it.

Cheers,

f


More information about the IPython-User mailing list