"I have a mind like a steel... uh... thingy." Patrick Logan's weblog.

Search This Blog

Friday, August 22, 2003

Annoying tuple syntax: partial solution

Victor Ng writes about Python's tuple syntax for length one tuples.

Not that I use a lot of explicit length one tuples, but here's something you can do instead. Redefine the tuple function to accept non-sequences. If so, just put them in a length one tuple.

def tuple(x):
        return original.tuple(x)
    except TypeError:
        return original.tuple([x])
Then you can express an explicit length one tuple as tuple(x)

No comments:

Blog Archive

About Me

Portland, Oregon, United States
I'm usually writing from my favorite location on the planet, the pacific northwest of the u.s. I write for myself only and unless otherwise specified my posts here should not be taken as representing an official position of my employer. Contact me at my gee mail account, username patrickdlogan.