Making a dict from two lists/tuples

Alex Martelli aleaxit at yahoo.com
Sat May 26 13:59:30 EDT 2001


"Jonathan Feinberg" <jdf at pobox.com> wrote in message
news:ofsgciqx.fsf at pobox.com...
> "Alex Martelli" <aleaxit at yahoo.com> writes:
>
> > temp=[decorate(x) for x in beep]
> > temp.sort()
> > beep[:]=[undecorate(x) for x in temp]
>
> I was wondering what a Schwartzian Transform might look like in
> Python. ;)

I used to call this one 'a Schwartzian Transform' even when I
coded it in Python, but then was chastised for this by purists
who claim the essence of a ST is being done in a single
statement, so I switched to 'decorate-sort-undecorate'...

We COULD fit it in a single statement with the usual little
Data class (assuming you always do have an instance of
that one around for any possible temporary use):

class Data:
    def __init__(self, val=None):
        self.val = val
    def set(self, val):
        self.val = val
        return val
    def get(self):
        return self.val
temp = Data()

beep[:] = map(undecorate, temp.set(map(decorate,beep))
    and temp.val.sort() or temp.val)

I think this is more Schwartzian in spirit, but less Pythonic
by far, although it IS handy to keep Data around to rub in
Perlite's faces "of COURSE I could use a oneliner if I really
WANTED to, I just CHOOSE to keep my code readable":-).


Alex






More information about the Python-list mailing list