[IPython-user] Not breaking a block on an indented line

Robert Kern rkern at ucsd.edu
Sun Oct 16 23:54:03 CDT 2005


Joshua Gilbert wrote:
> I think that this is a wishlist item, but I'd love to hear that it's
> already possible.
> 
> The goal is to be able to imput blocks that have indented lines
> without breaking the block. Example code:
> --------------------------------------------------------------------------------------------------------------------------
> class foo:
>     def __init__(self):
>         self.data = range(10)
> 
>     def printData(self):
>         print self.data
> 
> --------------------------------------------------------------------------------------------------------------------------
> This is good Python, it follows PEP 8.

Don't worry about following code standards in interactive shells. At
least, not until we're done with the notebook interface (but that won't
have this problem, either).

> The problem is that ipython
> doesn't recognize it:
> --------------------------------------------------------------------------------------------------------------------------
> In [1]: class foo:
>    ...:     def __init__(self):
>    ...:         self.data = range(10)
>    ...:
> 
> In [2]:     def printData(self):
> ------------------------------------------------------------
>    File "<console>", line 1
>      def printData(self):
>      ^
> SyntaxError: invalid syntax
> 
> 
> In [3]:         print self.data
> ------------------------------------------------------------
>    File "<console>", line 1
>      print self.data
>      ^
> SyntaxError: invalid syntax
> 
> 
> In [4]:
> --------------------------------------------------------------------------------------------------------------------------
> 
> The reason is pretty clear, ipython breaks the current block when it
> receives a line with only whitespace. I think that this is incorrect
> behaviour. The rule should be that a block ends when there is a
> following line with less indenting than the block requires.

The difference between the Python interpreter and IPython's is that
IPython treats all-whitespace lines and truly empty lines the same for
the purpose of deciding when to try to break. Given autoindent, this is
arguably a good rule; you don't have to backspace every time you end a
block. If find that I end blocks much more frequently than I care about
putting extra whitespace in my interactive input. It's not like any one
is going to read it six months later.

> python, on the other hand, does the right thing:
> --------------------------------------------------------------------------------------------------------------------------
> jgilbert at carthage:~/dev/prototypeGP/trunk$ python
> Python 2.3.5 (#2, Aug 30 2005, 15:50:26)
> [GCC 4.0.2 20050821 (prerelease) (Debian 4.0.1-6)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> 
>>>>class foo:
> 
> ...     def __init__(self):
> ...         self.data = range(10)
> ...
> ...     def printData(self):
> ...         print self.data
> ...

That doesn't work with my Python. You sure you're not missing some
whitespace there?

> --------------------------------------------------------------------------------------------------------------------------
> 
> Now, I'm happy to contribute code that implements the required
> functionality if it is not currently possible. To do so I will need a
> pointer as to where the changes need to be made.

It's probably not worth it. What we really need is real multi-line
editing. In the meantime, larger code segments like class definitions
are better handled with %edit and %run.

But if you insist, look in iplib.py in the method
InteractiveShell.interact() and trace through all of the various calls.
Keep in mind that it inherits from code.InteractiveConsole. This is one
of the hairiest parts of ipython.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the IPython-user mailing list