"What is the name of the function/method that called me?"

Andrew Dalke dalke at bioreason.com
Fri Oct 15 19:23:38 EDT 1999


Jim Althoff <jima at aspectdv.com>:
> So does
> 
> >>>try:
> >>>  raise: "Hack"
> >>>except "Hack"
> 
> only work by happenstance because the standard 
> Python implementation keeps a reusable list
> of literals (guessing) or because there is something
> formal in the language that says it must work?

I'm guessing the former, since:

>>> s = "A" + "B"
>>> try:
...     raise "AB"
... except s:
...     print "Ok"
... 
Traceback (innermost last):
  File "<stdin>", line 2, in ?
AB
>>> s
'AB'
>>> try:
...     raise "AB"
... except "AB":
...     print "Ok"
... 
Ok
>>> try:
...     raise s
... except "AB":
...     print "Ok"
... 
Traceback (innermost last):
  File "<stdin>", line 2, in ?
AB


So string literals work, likely because they are interned to
the same object, but equivalent strings don't work.  I believe
there is some history to things working this way because
exceptions in Python used to be string only.


BTW, my standard code for generating exceptions is

try:
  1/0
except ZeroDivisionError:
  print "Ok"

which doesn't have the string problem.

						Andrew Dalke
						dalke at acm.org




More information about the Python-list mailing list