[Grok-dev] Creating an object from a path

Sylvain Viollon sylvain at infrae.com
Fri Feb 6 11:01:57 EST 2009


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()

  Sylvain,

-- 
Sylvain Viollon -- Infrae
t +31 10 243 7051 -- http://infrae.com
Hoevestraat 10 3033GC Rotterdam -- The Netherlands
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 188 bytes
Desc: not available
Url : http://mail.zope.org/pipermail/grok-dev/attachments/20090206/e3b97aa9/attachment.bin 


More information about the Grok-dev mailing list