buggy python interpretter or am I missing something here?

Zachary Ware zachary.ware+pylist at gmail.com
Mon Jan 27 11:23:49 EST 2014


On Mon, Jan 27, 2014 at 1:17 AM, me <noone at all.net> wrote:
> It's the intendation specific requirements that throw me.  I haven't seen
> such a hork since RPG. ;^)

Best I can tell, the only thing RPG and Python have in common is the
fact that they're "programming languages", though I'm being a little
generous to RPG there.  I'll note that RPG (IV) is the only language
I've ever been formally trained in, unfortunately.  I've mostly
forgotten it though, so that's a plus :)

Python's significant indentation is really quite nice and intuitive.
The basic rule of thumb is if a line ends in a colon, the next line
needs to be indented one more level.  If you're continuing the same
logical block, keep the same indention level.  When a logical block is
finished (whether by a raise, continue, break, or return statement, or
just by "falling off the end" of the block), indent the next line one
level less.  Parentheses () (and brackets [] and braces {}) create
what amounts to a 'virtual line', \n characters are basically ignored
until the closing parenthesis is found (as long as they aren't in a
non-triple-quoted string literal).

As long as you don't try to mix tabs and spaces for your indentation
(either is fine on its own, but spaces are generally preferred), it's
very straightforward and allows you to better see the flow of the
program with no question as to whether there is anything hidden within
a block.  I've been using C more in the past two weeks than I really
expected to all this year, and have been bitten more than once by
missing braces.  Indenting intentionally is just a much cleaner,
clearer way to do things (in my opinion, of course).

>> Also, you can iterate over a (sys.argv) in a for
>> loop, replacing 'l', 'idx', and the while loop; something like
>>
>> for arg in a:
>>     if arg == '-h':
>>         print help # help being defined elsewhere...
>>     elif arg == '-hh':
>>         # no really, print help
>>         print 'help'
>
> Understood, except that some parameters take multiple elements...thus why
> I manually reference the indexes.

Try this on for size, then:

a_iter = iter(a)
for arg in a_iter:
    print('current', arg)
    if arg == '-#':
        print('next', next(a_iter))

> Yup.  Every language and platform has multiple arg parsing libraries.
> Didn't feel like taking the time to research any since most of my python
> ramblings are usually pyQT4 bindings to QT4 gui and postgresql apps.

As with most modules in the standard library, I would bet you can get
started using argparse in probably 15 minutes or less.  The online
documentation of the language and standard library is already quite
good, and there is constant effort going into improving it.  You might
also want to play around with the 'help()' function in the interactive
interpreter, it may have answered your original question without ever
having had to ask here:

>>> help(sys.exit)
Help on built-in function exit in module sys:

exit(...)
    exit([status])

    Exit the interpreter by raising SystemExit(status).
    If the status is omitted or None, it defaults to zero (i.e., success).
    If the status is numeric, it will be used as the system exit status.
    If it is another kind of object, it will be printed and the system
    exit status will be one (i.e., failure).

> Pointing out that sys.exit() raises a low level exception was the point I
> was missing.  Thx!

You're welcome, I'm glad I could help :)


As a general reply to the 'attitude' portion of this thread: "mutual
respect" is the name of the game here.  If you show respect (whether
you've already received it or not), you're likely to receive respect.
If not, don't be surprised by heated responses.  This applies equally
to everyone, I'm not pointing fingers at anyone in particular.

Regards,

-- 
Zach



More information about the Python-list mailing list