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

OKB (not okblacke) brenNOSPAMbarn at NObrenSPAMbarn.net
Sun Jul 8 20:31:03 CEST 2007


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



More information about the Python-list mailing list