[IPython-dev] cell magics

Thomas Kluyver takowl at gmail.com
Sat Feb 16 10:52:33 EST 2013

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

> Line and cell magics are normal python functions that take a string as
> their first input.  The string is either the rest of the line, or if
> there is no discernible string on the rest of the line, the string is
> taken as the rest of the cell.  As an example:
> %timeit(runs=10) 2+3
> which gets translated to time("2+3", runs=10)
> A cell magic is exactly the same function, only it is invoked by putting
> the string on the following lines, rather than on the same line, so the
> string is taken as the remainder of the cell:
> %timeit(runs=10)
> 2+3
> 5+6
> which gets translated to timeit("2+3\n5+6", runs=10)

As you noted in a later e-mail, we're not about to break backwards
compatibility, but it's worth thinking about the syntax. Here's my
perspective on magics:

- As Fernando says, they started off as commands with a deliberately
shell-like syntax, and many still work like this, e.g. %cd or %run.
- A couple of line magics accepted a Python statement as an argument
(%timeit, %prun). This is a convenient shorthand for functions that accept
code as a string.
- After the development of the notebook, magics were extended to include
cell magics, which are conceptually much more like your idea of 'string
decorators'. A primary use has been to include code written in other

Arguably, the newer uses don't have to fit into the same scheme as the
original command-like magics. But neither do they have to be separate, and
I think we're better off supporting one flexible scheme than adding a
second, incompatible scheme for cell magics.

Best wishes,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20130216/9ab04ae6/attachment.html>

More information about the IPython-dev mailing list