OT: Re: Just took a look in the perl newsgroup....
Bengt Richter
bokr at oz.net
Fri May 30 00:59:57 EDT 2003
On Thu, 29 May 2003 15:46:34 -0600, Steven Taschuk <staschuk at telusplanet.net> wrote:
>Quoth Bengt Richter:
>> On Wed, 28 May 2003 23:10:57 -0600, Steven Taschuk <staschuk at telusplanet.net> wrote:
[...]
> [...]
>> ====< caseswitch.py >=============================================
>> class Switch(object):
>> def __init__(self, *caseNames):
>> self.halias = {}
>> for name in caseNames:
>> try:
>> hname = '_%X' % hash(name)
>> except TypeError:
>> raise ValueError, 'Switch requires hashable case values'
>> exec ('class %s(Exception):pass'%hname) in self.__dict__
>> self.halias[name] = getattr(self, hname)
>> del self.__dict__['__builtins__']
>
>One point: why not just this?
>
> def __init__(self, *caseNames):
> self.halias = {}
> for name in caseNames:
> class AwfulHack(Exception):
> pass
> self.halias[name] = AwfulHack
Good point. Forgot that just differing ids of generated classes would suffice ;-/
>
>The hash code name is not used except in error messages, where it
>is not helpful in general. The proposed name at least tells the
>user that it's the programmer's fault. (A better name could be
>done with a metaclass __str__, I imagine.)
Good idea too. Are you sure you want to contribute towards making this
that slick ;-)
>
> [...]
>> Note that I had to make a choice re the tuple ambiguity. [...]
>
>Yes, that's slightly unfortunate, though it doesn't deserve a
>yikes.
>
>It's still abuse, though.
Well, at least I didn't include slice notation in specifying multiple cases ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list