[Grok-dev] Creating an object from a path

Paul Wilson paulalexwilson at gmail.com
Fri Feb 6 11:41:13 EST 2009


2009/2/6 Sylvain Viollon <sylvain at infrae.com>:
> On Fri, 6 Feb 2009 15:04:52 +0000
> Paul Wilson <paulalexwilson at gmail.com> wrote:
>
>> 2009/2/6 Sebastian Ware <sebastian at urbantalk.se>:
>> > If I have a string:
>> >
>> >   "<class 'trigger.promotion.Promotion'>"
>> >
>> > can I create an object of the type [trigger.promotion.Promotion]?
>>
>> You mean like:
>>
>> classobj = eval('trigger.promotion.Promotion')
>
>  I am not a fan of eval, you can provides anything you want and it
>  will be executed.
>
>  from zope.dottedname.resolve import resolve
>
>  classobj = resolve('trigger.promotion.Promotion')
>
>  Will just resolve the path to the object (and not evaluate anything).
>
>> instanceobj = classobj()
>

Yeah - eval should almost never be used I guess.. Thanks for the tip!

Paul


More information about the Grok-dev mailing list