[IPython-dev] cell magics

Thomas Kluyver takowl at gmail.com
Sat Feb 16 17:39:42 EST 2013

That's quite a lot to read, but while I think about it in more detail, a
couple of bits that jump out at me:

On 16 February 2013 22:07, Jason Grout <jason-sage at creativetrax.com> wrote:

> %time some_function
> now I want to add a few more things to the time run.  All I have to do
> is change where the string is:
> %time
> some_function
> some_other function
> and the lack of a string after %time tells me that I should look below
> %time for the string.  I don't have to constantly keep adjusting the %
> character(s).

For %%timeit, however, we use the remainder of the line as a setup
statement, before timing the body. Now you can argue that that's a bad
idea, but it's useful to have that distinction.

> P.S. Actually, after thinking about it more, since a single % is
> ambiguous syntax, I think I would prefer all string decorators be
> invoked with %%, which is invalid python syntax.  Then we won't have
> this problem:
> cd=5
> a=4\
> %cd
> or this problem:
> cd=4
> a="time: %d"\
> %cd

My input transformation work should handle such cases. Arguably it could be
done more easily if we used a syntax like %% that could never appear in a
valid Python statement, but it still wouldn't be trivial:

a = """

> ...

>  I think it's pretty clear which statement is more readable.  This focus
> on readability over brevity (remember, "Readability counts") is part of
> why python is so good in general.

And part of the reason IPython exists is to provide for the cases when you
do want brevity. Because there are times when you want to type '%run -d
foo' than 'pdb.runcall(execfile, "foo.py")'.

Of course, IPython has evolved - when it was only a shell, most of what you
entered was just for single use, so readability wasn't hugely important.
When you're writing & publishing a notebook, readability is a much bigger
deal. But I've yet to see a particular problem with people abusing magic
syntax and making notebooks hard to read.

> * backwards compatibility (with IPP and previous IPython versions, as
> well as IDL and matlab apparently?).  But I'll point out that we can
> easily support this too, in almost exactly the same invocation:
> %timeit('-r 5 -and -other -options') 2+3

Honestly, I wouldn't call that 'almost the same invocation'. Moreover, it
sacrifices the brevity without enforcing any corresponding gain in

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20130216/2bd26fb0/attachment.html>

More information about the IPython-dev mailing list