what a cheap rule
steve at holdenweb.com
Thu Nov 25 19:04:09 CET 2010
On 11/25/2010 10:15 AM, Yingjie Lan wrote:
> As I am thinking about it, it seems two
> conflicting intuition of code comprehension
> are at work here:
> Intuition #1: as if you raise an exception
> type, and then match that type.
> It seems that no instances
> are involved here (Intuitively).
> See an example code here:
> try: raise KeyError
> except KeyError: pass
> Intuition #2: you raise an exception
> instance, and then match an instance by
> its type. See an example code here:
> try: raise KeyError()
> except KeyError as ke: pass
> Those two comprehensions are not compatible,
> and thus the one that promotes correct
> understanding should be encouraged,
> while the other should be discouraged,
> and maybe even be made iliegal.
I prefer to treat those two cases as unified, by observing that if what
is raised in an exception class than an instance is created by calling
it with no arguments. So matching is always by the instance's type -
it's just that the instance creation can be implicit. I agree with you
that explicit is better.
Most of the syntactic variation you dislike is to allow existing code to
continue to work. Some of it is removed in Python 3, when backwards
compatibility could be ignored.
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon 2011 Atlanta March 9-17 http://us.pycon.org/
See Python Video! http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list