need an alternative to getattr()

Jon Ribbens jon+usenet at unequivocal.co.uk
Mon Aug 7 12:46:34 EDT 2006


In article <1154961041.067164.199520 at i42g2000cwa.googlegroups.com>, pranav.choudhary at gmail.com wrote:
> 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.

  globals()[heading].process(file_ptr)

or

  sys.modules[heading].process(file_ptr)

but note that, unless you are checking 'heading' against a 'known
good configuration keywords' list beforehand, you are trusting the
author of the configuration file.



More information about the Python-list mailing list