what a cheap rule

Steve Holden steve at holdenweb.com
Thu Nov 25 09:54:36 EST 2010


On 11/25/2010 3:12 AM, Yingjie Lan wrote:
> Sometimes the golden rule in Python of
> "explicit is better than implicit" is
> so cheap that it can be thrown away
> for the trouble of typing an empty tuple.
> 
I'm not sure that there *are* any golden rules. The "Zen of Python" is
intended to be guidelines, not rigid rules intended to constrain your
behavior but advice to help you write better code.

> Today when I am explaining that in Python 3,
> there are two ways to raise exceptions:
> 
> raise Exception
> 
> raise Exception()
> 
> and that the first one is the same
> as the second one, as Python will add the
> missing pair of parenthesis. 
> 
In fact this is noting to do with Python 3 - the same is true of Python
2, so this isn't new:

Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01)
[GCC 4.3.4 20090804 (release) 1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> raise KeyError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError
>>> raise KeyError("Here is the message")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'Here is the message'
>>>

> I felt their pain as they gasped.
> Before that, I have already explained 
> to them this piece of code:
> 
> try: raise SomeException()
> except SomeException:
>    print('Got an exception here')
> 
> by saying that the except-clause 
> will match anything 
> that belong to the SomeException class.
> 
Or any of its subclasses ...

> Without knowing this secrete
> piece of information (that a
> pair of parenthesis is automatically
> provided), the following code
> would be hard to understand:
> 
> try: raise SomeException
> except SomeException:
>    print('Got an exception here')
> 
> because the class object SomeException
> is not an instance of itself, so 
> a not-so-crooked coder will not
> consider a match here.
> 
It's a matter of understanding correctly how the interpreter operates
(and the interactive interpreter session is the ideal place to
investigate this). The 2.7 documentation for the raise statement says
(and this is not new):

"""
If the first object is an instance, the type of the exception is the
class of the instance, the instance itself is the value, and the second
object must be None.

If the first object is a class, it becomes the type of the exception.
The second object is used to determine the exception value: If it is an
instance of the class, the instance becomes the exception value. If the
second object is a tuple, it is used as the argument list for the class
constructor; if it is None, an empty argument list is used, and any
other object is treated as a single argument to the constructor. The
instance so created by calling the constructor is used as the exception
value.
"""

So the interpreter doesn't really "automatically provide a pair of
parentheses", but examines the exception object and instantiates it if
it is a class.

> So, the explicit is better than
> implicit rule is thrown out of
> the window so cheaply, 
> that it literally worth less 
> than an empty tuple!
> 
Surely an exaggeration. In fact current best practice (which you should
inform yourself of as best you can to help you in your teaching work -
so you are to be congratulated for bringing this question to the list)
is to always use explicit calls, with arguments specifying a tailored
message.

> Regards,
> 
> Yingjie
> 
> 
regards
 Steve
-- 
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 mailing list