[IPython-dev] cell magics

Hans Meine hans_meine at gmx.net
Wed May 29 03:53:30 EDT 2013


Hi Jason,

let me answer from my perspective, too:

It all boils down to the fact that magics are exactly *not* Python.  (I guess) They're called magics because they "magically" transform into valid, longer python code.  Their whole point is not to perform something that's not possible without magics, but to offer a really short, easy to type (-> accessible) way to perform some action.

The magics I use most often are %run, %debug, %history, %pylab, %gui and %cpaste (in the terminal), in the past also %store and %macro.

They allow me to do stuff interactively really quickly, without first getting references to some responsible objects or namespaces.

Only very seldomly I use the fact that I can use them in .ipy files to be run through IPython, and that's only for short-lived files that reproduce some debugging session.

Am 16.02.2013 um 23:07 schrieb Jason Grout:

> With our proposal, %<tab> could still just complete with registered 
> string decorators (with even the exact same registration system, so you 
> can register things outside of the user's namespace).  The difference is 
> that if I wanted to run a string decorator that wasn't in the registered 
> namespace, but I had just defined in my user namespace, I wouldn't have 
> to pollute the registered namespace for my one invocation.  I could just 
> run %my_function directly.  I think this is particularly important 
> because the registered % namespace is a flat list of names, so it's not 
> a very organized namespace (which is exactly a problem you have with 
> Sage's philosophy, ironically :).

With magics, I don't think about namespaces.  There shouldn't be too many of them anyhow.

> But the real issue here is that IPython has two different syntaxes for 
> the users to invoke string decorators.  That means that I, as a user, 
> constantly have to remember if a command is one, or the other, or both, 
> because the very first two characters are different between the two. 
> For example, just now I typed %ru<tab>, and I see from the completions 
> that ruby is only a cell decorator.  So now I have to backspace and put 
> that extra % in there. …

I appreciate the thought you put into this argument, but to me this is a non-issue.  You can always come up with cases where you don't remember the name of something and need to backspace.  For me, cell magics are clearly distinct from the others, and I would never look for %ruby. ;-)

> With our proposal, these issues go away.  All string decorators are 
> invoked with %.  The difference between a line or cell decorator is 
> determined by whether or not there is a string on the line (just as the 
> difference between …

Here, you make two assumptions that do not hold:
1) %magics always take arguments.  Many of them perform important (often, listing) actions without arguments, and with your proposal, I would have to at least press enter twice in the terminal.
2) %%cell_magics don't take arguments on the same line.  You were already given counterexamples for that one.

> (a) users are comfortable with bash-like syntax.  With the rising 
> popularity of IPython, and especially with the rising popularity of the 
> notebook, I think we're going to see more and more non-unix users that 
> see the bash-like syntax as one more *new* thing to learn, rather than 
> something that is already familiar in a different context.  In fact, I 
> look at the %timeit syntax for example, and I have to try to remember 
> what the options are each time.

Isn't that the case with most API?  Just use '%timeit?' and be happy with IPython. :-)

>  Compare:
> 
> %timeit -r5 2+3
> 
> %timeit(runs=5) 2+3
> 
> 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.

Readibility does count (and I advocate that _a lot_), but not for the commands I type in an IPython console.  There, I want to type as fast as possible, so brevity trumps.  (Also, lack of parentheses and commas is a big plus for typing, depending on your keyboard layout at least.)

> I look at the %R option syntax, and I absolutely *have* to go to the 
> help to see what is going on.

Even worse are the lines below!! :-) (Hint: I don't speak R.)

As soon as you put magics in a permanent document (such as the notebook), you are interested in --long option names, of course.  Anyway, I wonder how many %magics and options you will realistically use in notebooks.  (Many of the aforementioned %magics don't really belong there.)

Best regards
  Hans




More information about the IPython-dev mailing list