[Python-Dev] RE: PEP-317

sismex01@hebmex.com sismex01@hebmex.com
Tue, 10 Jun 2003 09:26:34 -0500


> From: Aahz [mailto:aahz@pythoncraft.com]
> Sent: Tuesday, June 10, 2003 9:23 AM
> 
> Yeah.  I've been thinking a bit, and while the proposed change makes
> some sense for the case where you're passing an argument to an
> exception, it makes this look uglier:
> 
> class Found(Exception):
>     pass
> 
> try:
>     for record in records:
>         for field in record.fields:
>             if data in field.data:
>                 raise Found()
> except Found:
>     # handle success
> else:
>     # handle failure
> 
> I find that extra set of parentheses unnecessary and ugly.  
> It implies a callable where it's not really being used that
> way, particularly in the asymmetry between the ``raise`` and
> the ``except``.
>

On the other hand, I read it quite clear; the extra set
of parenthesis you find so ugly, imply to me that an
instance of Found is being raised, and that except: is
going to catch all instances of Found.  It's a matter
of taste, that you (aesthetically) like it or not,
but it's a matter of consistency and correctness that
it should be there or not.

>
> If this change goes through, I might start doing
> 
> class Found(Exception):
>     pass
> Found = Found()
>

You won't catch anything in that case, check this out:

Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help

>>> class Found(Exception):
	pass

>>> Found = Found()
>>> def fnEx():
	try:
		print "Fire in the hole!"
		raise Found
	except Found:
		print "Fire extinguished."

		
>>> 
>>> 
>>> fnEx()
Fire in the hole!
Traceback (most recent call last):
  File "<pyshell#21>", line 1, in ?
    fnEx()
  File "<pyshell#18>", line 4, in fnEx
    raise Found
Found
>>> 
>>> isinstance(Found,Found)
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in ?
    isinstance(Found,Found)
TypeError: isinstance() arg 2 must be a class or type
>>> 


You can't catch instances of the Found.__class__
by referring to Found, you'd have to do something
like this:


>>> 
>>> def fnEx():
	try:
		print "Fire in the hole!"
		raise Found
	except Found.__class__:
		print "Caught."

		
>>> 
>>> fnEx()
Fire in the hole!
Caught.
>>> 


There it did get caught, but it seems quite uglier
than an extra pair of parenthesis.

just my 2 cents.

-gca

pd: Please excuse this unnecessary, hideous and wasteful
    legalese. :-(
--
Advertencia:La informacion contenida en este mensaje es confidencial y
restringida, por lo tanto esta destinada unicamente para el uso de la
persona arriba indicada, se le notifica que esta prohibida la difusion de
este mensaje. Si ha recibido este mensaje por error, o si hay problemas en
la transmision, favor de comunicarse con el remitente. Gracias.