[IPython-dev] splitting a class definition across cells in notebook

William Stein wstein at gmail.com
Thu Oct 17 13:01:05 EDT 2013


Hi,

I'm not sure how easy this is in IPython, but you could define a new
%%mode that syntax highlights normally and instead of evaluating code,
it appends that code as a string to some variable.  Then do

[markdown]
[code cell starting with %%mode]
[markdown]
[code cell starting with %%mode]
etc.

Finally, at the end, simply exec that string.  Alternatively, append
to a file in each step, instead of a string, then exec the file at the
very end.

I don't know if IPython has a way of temporarily enabling a default
mode, but if it does you could enable it at the top of this, do
everything, then disable it at the bottom, so you don't have to
explicitly put %%mode at the top of each cell.

(I'm guessing IPython can do these things, since they're natural to
implement, and I implemented similar things in cloud.sagemath
worksheets.)

Anyway -- think of this question as a challenge for how to use/better
develop core features...

 -- William

On Thu, Oct 17, 2013 at 9:47 AM, Aaron Meurer <asmeurer at gmail.com> wrote:
> You could just do it the old fashioned way and use comments.
>
> If you care about markdown formatting, what I would do is do the whole thing
> in a markdown cell, with the code of the class in different code blocks, and
> then define the actual thing in a code cell at the very end (or before the
> markdown cell if that flows better).
>
> Defining the class ad hoc as other people have suggested is only going to
> make your complicated class even harder to understand, especially if your
> audience doesn't fully understand how methods bind to classes in python.
>
> Aaron Meurer
>
> On Oct 17, 2013, at 8:13 AM, Thomas Kluyver <takowl at gmail.com> wrote:
>
> On 17 October 2013 02:35, Felix Breuer <felix at fbreuer.de> wrote:
>>
>> I am working on an IPython notebook in which I want to explain the
>> definition of a class in detail. (Think literate programming.) In
>> particular, I want to interleave the definition of the individual methods of
>> a class with Markdown cells explaining in detail what is going on. However,
>> as far as I have been able to find out, I have to put the entire class
>> definition in a single cell - and there is no way to amend this definition
>> later on. Is there any way around this?
>
>
> One rough way to do this would be to define each method as a function, and
> assign it to the class at the end of its cell. Like this:
>
> def mymethod(self, arg):
>     ...
>
> MyClass.mymethod = mymethod
>
> That's not ideal, but it should allow you to divide a class up into separate
> cells.
>
> Thomas
>
> _______________________________________________
>
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>



-- 
William Stein
Professor of Mathematics
University of Washington
http://wstein.org



More information about the IPython-dev mailing list