Something like the getattr() trick.

faulkner faulkner891 at gmail.com
Sat Feb 10 16:30:13 EST 2007


On Feb 10, 3:34 pm, Ayaz Ahmed Khan <a... at dev.slash.null> wrote:
> I'm working with the following class heirarchy (I've snipped out the code
> from the classes):
>
> class Vuln:
>         def __init__(self, url):
>                 pass
>
>         def _parse(self):
>                 pass
>
>         def get_link(self):
>                 pass
>
> class VulnInfo(Vuln):
>         pass
>
> class VulnDiscuss(Vuln):
>         pass
>
> def main(url):
>         vuln_class = ['Info', 'Discuss']
>         vuln = Vuln(url)
>         vuln._parse()
>         for link in vuln.get_link():
>                 i = VulnInfo(link)
>                 i._parse()
>                 d = VulnDiscuss(link)
>                 d._parse()
>
> Is there a way to get references to VulnInfo and VulnDiscuss objects using
> something like the getattr trick? For example, something like:
>
>         for _class in vuln_class:
>                 class_obj = getattr('Vuln%s' % (_class,) ..)
>                 a = class_obj(link)
>                 a._parse()
>
> getattr() takes an object as its first argument. I can't seem to figure
> out how to make it work here.
>
> --
> Ayaz Ahmed Khan
>
> A witty saying proves nothing, but saying something pointless gets
> people's attention.

eval('Vuln' + _class)
or,
Vuln.Discuss = VulnDiscuss
getattr(Vuln, _class)




More information about the Python-list mailing list