On Fri, Feb 22, 2013 at 9:11 PM, Rohit Mediratta <rohit_m...@hotmail.com> wrote: > Heres the pseudo code of what I want to do: > > def rerun(testBlock) : > op = testBlock.split('.') > module = op[0] ; className = op[1] > reload(module) > # testBlock is a string, so it needs evaluation! > newObject = testBlock() > > rerun('ModuleName.className')
You can use __import__, and extend it to handle packages: try: reload except NameError: from imp import reload # 3.x def rerun(clsPath, *args): modPath, clsName = clsPath.rsplit('.', 1) mod = __import__(modPath, fromlist='not empty') reload(mod) cls = getattr(mod, clsName) return cls(*args) The fromlist option only cares whether or not the sequence is empty. If it's not empty you get the leaf (e.g. 'xml.etree.ElementTree' => ElementTree). If it's empty you get the base package (e.g. 'xml.etree.ElementTree' => xml). _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor