OT: Re: Just took a look in the perl newsgroup....
Steven Taschuk
staschuk at telusplanet.net
Thu May 29 01:10:57 EDT 2003
Quoth Bengt Richter:
> On Wed, 28 May 2003 14:17:37 -0600, Steven Taschuk <staschuk at telusplanet.net> wrote:
> >Quoth Bengt Richter:
[...]
> >> ## a local case structure with rebinding in local scope
> >> try: raise `x`
> >> except '1':
[...]
> >Inherently fragile due to reliance on interning of repr(x). Won't
> >work at all if/when string exceptions go away.
>
> Which version requires interning of repr(x)? It looks like the code is less
> efficient than the corresponding if/elif/else code, however.
It is the quoted try/except version that relies on interning. For
example:
>>> x = 12345
>>> try:
... raise `x`
... except '12345':
... print 'case 12345'
...
Traceback (most recent call last):
File "<stdin>", line 2, in ?
12345
Because string exceptions are caught by identity, not equality,
and, as it happens, '12345' doesn't get interned.
[...]
> class Switch(object):
> def __init__(self, *caseNames):
> for name in caseNames:
> if not isinstance(name,str): name = `name`
> exec ('class _%s(Exception):pass'%name) in self.__dict__
> del self.__dict__['__builtins__']
> def __call__(self, name):
> raise getattr(self, '_%s'%name, ValueError)
Yikes again. This one depends on repr(x) fitting into identifier
syntax, so I couldn't use it with tuples (to pick one example of
many).
[...]
--
Steven Taschuk 7\ 7'Z {&~ .
staschuk at telusplanet.net Y r --/hG-
(__/ )_ 1^1`
More information about the Python-list
mailing list