How do I call anonymous classes from imported modules?

JoeSox joesox at gmail.com
Mon Aug 20 06:04:53 CEST 2007


On 8/19/07, James Stroud <jstroud at mbi.ucla.edu> wrote:
> Quick and dirty (you could also use a try: except:):
>
> f = __import__(module_name)
> for anobj in f.__dict__.values():
>   if hasattr(anobj, 'do_foobar'):
>     anobj.do_foobar()
>
> Note that this does not test whether anobj is a class as this would
> entail a type-check, which hints to poor design.

Thanks for the help.  The __dict__.values() is something I have not
previously looked into but it does not work as desired because python
wants an instance of the class.  How can I cast an object if I don't
know what type of object it is (example.ticker, in this debug)?

>>> modname = sensorsList[0].__name__
>>> m = __import__(modname)
>>> for anobj in m.__dict__.values():
	if hasattr(anobj, 'do_foobar'):
		anobj.do_foobar()

		
Traceback (most recent call last):
  File "<pyshell#76>", line 3, in ?
    anobj.do_foobar()
TypeError: unbound method do_foobar() must be called with ticker
instance as first argument (got nothing instead)



More information about the Python-list mailing list