need an alternative to getattr()

pranav.choudhary at gmail.com pranav.choudhary at gmail.com
Mon Aug 7 10:30:41 EDT 2006


Hi,

AIM: I have a config file that contains configuration under headings
like this:

heading1:
<configuration 1>
<configuration 2>
heading2:
<configuration 1>
<configuration 2>
...
...

i parse this file to get heading1, heading2, etc and then i want to
call heading1.process(), heading2.process(), etc.
What i am trying to do is: (this is not the exact code, it just
represents what i am trying to do)

import heading1
import heading2
While True:
    heading = get_next_heading(file_ptr) # This func will return
"heading1", then "heading2"(on next call)
    if heading = "":
        break
    getattr(heading, "process")(file_ptr) # The problem, as you would
have noticed, is that the first
# argument to getattr is a string!!

Is there an alternatice to getattr() that will solve my problem, or is
there another way to do it.

-pranav




More information about the Python-list mailing list