Newbie: needs help with ...

Bengt Richter bokr at oz.net
Fri Nov 8 00:29:07 EST 2002


On Thu, 7 Nov 2002 19:46:44 -0800, Terry Hancock <hancock at anansispaceworks.com> wrote:

>On Thursday 07 November 2002 07:26 pm, python-list-request at python.org wrote:
>> I'm trying to print the contents of a list.
>> 
>> Python displays "..." until I press enter. Why is it waiting for me to 
>press enter ?
>> 
>> >>> for x in l2[:5] : print x
>> ...
>
>I'm not exactly sure whether:
>
>for x in l2[:5]: print x
>    print x, x
>
>would be legal (it's certainly bad style), so maybe the
Apparently not:

 >>> l2 = range(10)
 >>> for x in l2[:5]: print x
 ...     print x, x
   File "<stdin>", line 2
     print x, x
     ^
 SyntaxError: invalid syntax

>interpreter ought to figure out that there won't be anything
>more in the loop.  But what the "..." is about is the way the
>interactive interpreter decides when a loop is actually
Not just loops, but any first-level indented block

>complete:
>
>>>> if x in l2[:5]:
>...        print x
>...        print "x = %d" % x
>...
>
>That last "..." with no content tells the interpreter you're done
>so it can actually run the loop.  (So you can't have any
>blank lines when you're pasting code into the interpreter
Ah, I misread that. But you can have blank lines. Pasting three:
(first using """ to have something I can block copy for pasting)

 >>> """
 ... print 'first'
 ...
 ... print 'third'
 ...
 ... """
 "\nprint 'first'\n\nprint 'third'\n\n"

Ok, here I paste the three lines including EOL on the third:

 >>> print 'first'
 first
 >>>
 >>> print 'third'
 third

You can paste them. It's just that they take effect when the blank
lines are encountered. The serious requirement is just the opposite:
you _must_ have a blank line to signal a dedent to level zero if you
are going to paste a block with additional lines following after
the dedent, which I guess you mean by "to test it".

>to test it, and each loop must have a blank line as well as
>a dedent). 
>
A workaround is to put an "if 1:" in front of the bunch of code with
multiple indent/dedent from/to level zero, to make them into a single block.
E.g., (at least in console window of NT using python 2.2.2)
BTW, I definitely think this could be smarter, but it must be hard to
make it so or I'm sure it would be. Below I pasted the line groups
all at once (I think ;-):

 >>> def foo():    # line 1
 ...     print 'Hi from foo'  # line 2
 ... foo()         # line 3
   File "<stdin>", line 3
     foo()         # line 3
       ^
 SyntaxError: invalid syntax

I don't see why it couldn't recognize the dedent and process the def and
then the last line. Workaround:

 >>> if 1:         # line 0
 ...     def foo(): # line 1
 ...         print 'Hi again' # line 2
 ...     foo()      # line 3
 ...
 Hi again


 >>> def foo():    # line 1
 ...     print 'Hi from foo'  # line 2
 ... # this is not blank
 ... foo()
   File "<stdin>", line 4
     foo()
       ^
 SyntaxError: invalid syntax


 >>> def foo():    # line 1
 ...     print 'Hi from foo'  # line 2 -- next line blank
 ...
 >>> foo() # previous line blank
 Hi from foo

 >>> # Also, why can't it consume a comment like this and prompt with >>> ?
 ...
 >>>

>This is a difference between the interactive and  script
>modes of operation of python.  You see the same thing with
>tcsh and bash, BTW (though the prompt symbols are
>different).
>

I'm not going to check on it ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list