[IPython-dev] cell magics
Sat Feb 16 09:52:33 CST 2013
On 16 February 2013 03:21, Jason Grout <email@example.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:
> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the IPython-dev