In search of python idiom for accessing arbitrary fields at run time

mshiltonj mshiltonj at gmail.com
Sun Jul 8 14:38:34 EDT 2007


On Jul 8, 2:31 pm, "OKB (not okblacke)"
<brenNOSPAMb... at NObrenSPAMbarn.net> wrote:
> mshiltonj wrote:
> > In python, I'm doing something like this:
>
> >  def three_fields(self, field1, field2, field3):
> >       for field in (field1, field2, field3):
> >             value = eval('self.' + field) # this is the one I'm
> > interested in
> >             [...]
>
> > This seems to do what I expect it to do. I'm wondering if that's the
> > preferred or standard way to do this in python. I wasn't sure how to
> > tease the answer to this question out of Google.
>
>         I believe you are looking for the getattr function.
>
> --
> --OKB (not okblacke)
> Brendan Barnwell
> "Do not follow where the path may lead.  Go, instead, where there is
> no path, and leave a trail."
>         --author unknown


Doh. I figured it'd by FAQish. :-/  Thanks.




More information about the Python-list mailing list