Simulating call-by-reference

Alex Martelli aleax at mail.comcast.net
Thu Nov 17 15:31:08 EST 2005


Dan Sommers <me at privacy.net> wrote:
   ...
> 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.

I think you mean the Bunch idiom, rather than the Borg one (which has to
do with having instances of the same class share state).

Personally, I would rather pass myvar as well as the attribute names,
and set them with setattr, as I see some others already suggested.


Alex



More information about the Python-list mailing list