multiline prototyping on command line (oops)

greg andruk meowing at banet.net
Sat Oct 30 06:14:52 EDT 1999


On 30 Oct 1999, Eric Smith wrote:

> Yeah, it is a nice "shell" interface for testing, however one liners are
> useful:
> 1. I use their history arrows to access them from the unix shell

Yup, but the interactive Python thingy has that too if you've got the
readline support compiled in.

> 2. You can use the unix fc for editing long ones

Okay, but orking inside either emacs or idle will get you the same
functionality.

> 3. You can paste them into your scripts.

SAame as for #2.

> 4. They are easy to go back to and edit etc.

Okay, mostly it sounds like you're wanting to use the tools that work well
with Perl the same way in Python.  You're going to be unhappy trying to
get there from here.  It could well be that Perl is more the language for
you than Python is; coming from the other end, the brackets and stuff that
make Perl work the way you like are among the reasons I like to use Python
when I can.

> This would certainly be high on my Python wish list (once I started
> wishing).  Especially if one was learning from fresh and had a very short
> attention span :(

The problem with making `python -c' go is that the position of things on
lines is important.  Taking the oriinal example, this will kind of get
what you want:

 meowing:~$ python -c "r='de'; print r == 'whatever' or r == 'de'"
 1

But phrasing it in the original way you presented it takes some oddness.
A straight translation will fail:

 meowing:~$ python -c "r='de'; if r == 'whatever' or r == 'de'): print 'match'"
   File "<string>", line 1
     r='de'; if r == 'whatever' or r == 'de': print 'match'
             ^
 SyntaxError: invalid syntax

OTOH, lots of shells tend to be smart enough to allow two-liners, but this
kind of thing gives me the screaming willies:

 meowing:~$ python -c "r='de'
 if r == 'whatever' or r == 'de': print 'match'"
 match

>  > >>> r in ('whatever', 'de')
>  > 1
> kewl
>  > >>> # and just for the heck of it...
>  > ... r == 'whatever' or 'de'
>  > 'de'
> For me this _always_ prints the token after the `or'.

Oh yeah?

>>> r = 'whatever'
>>> r == 'whatever' or 'de'
1

So there =)  This can be nifty to build an if-like thing into an
assignment.  Works in Perl too, BTW.





More information about the Python-list mailing list