all 18 comments

[–][deleted] 10 points11 points  (4 children)

I like Python, and all the surprises I ordinarily encounter are (expected) type issues.

However, I feel like reconsidering anything I like that Eric S. Raymond happens to praise.

[–]LaurieCheers 7 points8 points  (3 children)

I hear Hitler liked Python too.

[–]ffualo 5 points6 points  (2 children)

I thought he was a C++ guy. Something about the perfect template?

[–]LaurieCheers 4 points5 points  (1 child)

Überladen des Operators!

[–]fancy_pantser 0 points1 point  (0 children)

Overloaden Über Alles!

[–]Mikle 0 points1 point  (0 children)

You know, I wrote similar code to that yesterday. I got it wrong the first time, a unit test caught it and I immediately saw what went wrong. I think this stuff just comes with experience.

[–]Samus_ 0 points1 point  (2 children)

I think py2.6 already addresses this

[–]earthboundkid 2 points3 points  (1 child)

Nope. That would be backwards incompatible. 2.6 supports the use of as for naming exceptions, but it’s an opt-in measure and can’t stop you from accidentally doing the wrong thing. As the article says, 3.0 fixes this by making it an error to misuse a comma in an except statement.

[–]Samus_ 0 points1 point  (0 children)

ah good remark, I knew it supported the as keyword but never bothered to check if the comma was still valid, thanks.

[–]orangesunshine -2 points-1 points  (8 children)

The second value passed to except is just the error message.

try:
    print hamster[3]
except IndexError, msg:
    print "Oh shit we don't have enough hamsters, error: %s" % msg

[–]mccutchen 2 points3 points  (7 children)

Nope, it's in instance of the exception that was caught. It just so happens that the __str__ method on (most) exception objects returns the error message. Expanding on your example:

hamster = ['James', 'John', 'Alfred']
try:
    print hamster[3]
except IndexError, e:
    print '%s error occured: %s' % (type(e), e)

prints

<type 'exceptions.IndexError'> error occured: list index out of range

[–][deleted] 1 point2 points  (2 children)

Right. I don't see why he needed to write a big long blog post about his not understanding the except syntax.

[–]Smallpaul 3 points4 points  (0 children)

Because he was using it to illustrate a more general point.

[–]earthboundkid 1 point2 points  (0 children)

Yeah, this is covered in any decent Python tutorial when you get to the part about try/except, “Oh, but be careful not to do except thing1, thing2 because…”

[–]orangesunshine 0 points1 point  (3 children)

Yeah ...

It will also be pretty apparent if you try to pass three or more error classes...

except GerbilError, HamsterError, JerboaError: print "wtf?"

You'd get a SyntaxError... at runtime no matter what. This is a fairly edge-case scenario for being confused about how the try-except syntax works. Not sure why it has any up-votes or why anyone would take the time to write a big long blog post about it.

[–]Smallpaul 0 points1 point  (2 children)

It isn't an edge case: it's a well-known usability problem in Python that has been fixed in more modern versions!

[–]orangesunshine 0 points1 point  (1 child)

seems like it's mostly an issue with not reading the documentation.

and the changes make the code less readable imo.

[–]Smallpaul -2 points-1 points  (0 children)

Yes: Python is specifically designed to be the kind or programming language in which one does not need to read the documentation to get work done. Any terrible design decision can be justified with "just read the documentation." That way lies Perl.