Simulating call-by-reference

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


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

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

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

>     for (x,y) in l:
>       m = re.search(x, y)
>       if m:
>            y = m.group(1)

> 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 = re.search(regexp, data)
        if m:
            results[key] = m.group(1)

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
results.foo, etc.

Regards,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>



More information about the Python-list mailing list