OT: Re: Just took a look in the perl newsgroup....

Steven Taschuk staschuk at telusplanet.net
Thu May 29 17:46:34 EDT 2003


Quoth Bengt Richter:
> On Wed, 28 May 2003 23:10:57 -0600, Steven Taschuk <staschuk at telusplanet.net> wrote:
  [...]
> >and, as it happens, '12345' doesn't get interned.
> ITYM `12345` doesn't get "interned" (neither does '1234'+'5')

Urm, sort of.  I meant that a string object '12345' created by the
code "`12345`" isn't interned.  The equal string object created by
the code "'12345'" does, as it happens, which I assume to be your
point.

I should certainly have said that '12345' doesn't get interned
*here*.

  [...]
> ====< 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

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.)

  [...]
> 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.

-- 
Steven Taschuk             "[W]e must be very careful when we give advice
staschuk at telusplanet.net    to younger people: sometimes they follow it!"
                             -- "The Humble Programmer", Edsger Dijkstra





More information about the Python-list mailing list