On Nov 26, 1:10 am, Steven D'Aprano <steve +comp.lang.pyt...@pearwood.info> wrote: > On Thu, 25 Nov 2010 08:15:21 -0800, Yingjie Lan wrote: > > Intuition #1: as if you raise an exception type, and then match that > > type. > > It seems that no instances > > are involved here (Intuitively). > > Your intuition is not my intuition, nor does it match what Python > actually does. You can only go so far on *guessing* what a programming > statement does, sometimes you need to actually read the Fine Manual. > > > See an example code here: > > > try: raise KeyError > > except KeyError: pass > > As the documentation states, this creates an instance of KeyError. > > raise evaluates the first expression as the exception object. > It must be either a subclass or an instance of BaseException. > If it is a class, the exception instance will be obtained when > needed by instantiating the class with no arguments. > > http://docs.python.org/py3k/reference/simple_stmts.html#the-raise-sta... > > So there is no semantic difference between "raise KeyError" and > "raise KeyError()". Why should there be? What practical difference would > you expect? >
There *can* be a difference though. >>> raise UnicodeDecodeError Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: function takes exactly 5 arguments (0 given) :-) > > You seem to have misunderstood both forms of the raise statement. Should > we make exceptions illegal because you can't correctly guess what they do? Sometimes people not being able to understand them is a good reason for making things illegal (or rather for not making them legal in the first place). I don't think it applies to this particular case though. All the best, Michael Foord -- http://www.voidspace.org.uk/ -- http://mail.python.org/mailman/listinfo/python-list