converting from perl: variable sized unpack

Alex Martelli aleaxit at
Mon Jul 16 11:17:13 EDT 2001

"Roy Smith" <roy at> wrote in message
news:9iuq95$3e0$1 at
> Alex Martelli <aleaxit at> wrote:
> > I find I can rarely throw away the match-objects in this cavalier
> > way, because they carry much, often-needed information -- so, I
> No it's not, but the problem is not that match returns None, the
> problem is that Python doesn't allow assignment as a side effect.

If I wanted "assignment as a side effect" it would of course be
trivial for me to code it.  I far prefer to have failure be
signaled by exceptions -- a much more general & solid idiom.

> Imagine if you could write it this way:
>     if mo = re1.match(thestring)
>         dostuff(mo)
>     elif mo = re2.match(thestring)
>         dootherstuff(mo)
>     elif mo = re3.match(thestring)
>         etcetcetc(mo)
>     else:
>         nomatchatall()
> Wouldn't that be elegant and clean and obvious?
> After 4 years of using Python, I still find the lack of any way to do
> an atomic "assign and test" unbelievably constraining.

Surely you ARE jesting, right?  After (I believe it was) four DAYS of
Python, I had already placed in my the few lines
(just typing them from memory now, as I've since removed them):

    class DataHolder:
        def __init__(self, value=None): self.value = value
        def set(self, value): self.value = value; return value
        def get(self): return self.value

to help me transliterate some existing C and Perl code which used
what you call "atomic assign and test" -- a q&d hack until I could
find the time to do it more Pythonically (and, actually, LEARN the
Pythonic ways of doing things -- after four days, it was clear to
me that I had not yet mastered them:-).  So, IF I considered it
elegant to set something and test it too, I could easily code, e.g:

   if data.set(re1.match(thestring))
   elif data.set(re2.match(thestring))
   elif data.set(re3.match(thestring))

So far, so obvious, I hope -- whence my near-certainty that
your bemoaning a 'lack' so trivially fixed as 'unbelievably
constraining' must be some kind of deadpan humor whose point
I'm missing -- if half a dozen lines in are
all it takes to eliminate something "unbelievably constraining",
how believable is it that one's really feeling constrained?-)

Hmmm, maybe I should write this up as a Cookbook entry (not
that there's much to write about...), just so I can give a
pointer to it in the future rather than typing it in again
and again on each such discussion...:-).  Done -- see


More information about the Python-list mailing list