Simulating call-by-reference

Dan Sommers me at
Thu Nov 17 12:07:04 CET 2005

On Thu, 17 Nov 2005 10:03:50 GMT,
Rikard Bosnjakovic <bos at> wrote:

> What I want is to rewrite it to something like this:

>     l = [ (re1,,
>           (re2,,
>           (re3, myclass.baz),
>         ]

>     for (x,y) in l:
>       m =, y)
>       if m:
>            y =

> But since Python doesn't work that way, that idea is doomed. What I'm
> looking for are other (better) ways or pointers to accomplish this
> task of cleanup.

Put the results into a dictionary (untested code follows!):

    l = [ (re1, 'bar'),
          (re2, 'foo'),
          (re3, 'baz'),
    results = {}
    for (regexp, key) in l:
        m =, data)
        if m:
            results[key] =

Now you can access the results as results['foo'], etc.  Or look up the
Borg pattern in the ASPN cookbook and you can access the results as, etc.


Dan Sommers

More information about the Python-list mailing list