Show More
@@ -120,11 +120,36 b" IPython's 'magic' functions" | |||
|
120 | 120 | |
|
121 | 121 | The magic function system provides a series of functions which allow you to |
|
122 | 122 | control the behavior of IPython itself, plus a lot of system-type |
|
123 | features. All these functions are prefixed with a % character, but parameters | |
|
124 | are given without parentheses or quotes. | |
|
123 | features. There are two kinds of magics, line-oriented and cell-oriented. | |
|
124 | ||
|
125 | Line magics are prefixed with the % character and work much like OS | |
|
126 | command-line calls: they get as an argument the rest of the line, where | |
|
127 | arguments are passed without parentheses or quotes. For example, this will | |
|
128 | time the given statement:: | |
|
129 | ||
|
130 | %timeit range(1000) | |
|
131 | ||
|
132 | Cell magics are prefixed with a double %%, and they are functions that get as | |
|
133 | an argument not only the rest of the line, but also the lines below it in a | |
|
134 | separate argument. These magics are called with two arguments: the rest of the | |
|
135 | call line and the body of the cell, consisting of the lines below the first. | |
|
136 | For example:: | |
|
137 | ||
|
138 | %%timeit x = numpy.random.randn((100, 100)) | |
|
139 | numpy.linalg.svd(x) | |
|
140 | ||
|
141 | will time the execution of the numpy svd routine, running the assignment of x | |
|
142 | as part of the setup phase, which is not timed. | |
|
143 | ||
|
144 | In a line-oriented client (the terminal or Qt console IPython), starting a new | |
|
145 | input with %% will automatically enter cell mode, and IPython will continue | |
|
146 | reading input until a blank line is given. In the notebook, simply type the | |
|
147 | whole cell as one entity, but keep in mind that the %% escape can only be at | |
|
148 | the very start of the cell. | |
|
125 | 149 | |
|
126 | 150 | NOTE: If you have 'automagic' enabled (via the command line option or with the |
|
127 |
%automagic function), you don't need to type in the % explicitly |
|
|
151 | %automagic function), you don't need to type in the % explicitly for line | |
|
152 | magics; cell magics always require an explicit '%%' escape. By default, | |
|
128 | 153 | IPython ships with automagic on, so you should only rarely need the % escape. |
|
129 | 154 | |
|
130 | 155 | Example: typing '%cd mydir' (without the quotes) changes you working directory |
General Comments 0
You need to be logged in to leave comments.
Login now