Loading new code...

Olivier Dagenais olivierS.dagenaisP at canadaA.comM
Sun Aug 13 23:14:15 EDT 2000


> >...so, knowing that I have just loaded [from the database] a class
> >exec ( "self.aNewBlarg = Blarg ( 'arg1', 'arg2' )" )
> Better than that. You can just say
>   blarg = Blarg(arg1, arg2)

Yes, I know that, thank you, but I meant to say that I may not know what the
name of the class I am loading will be, hence I would be building up the
construction as a string:

construction = "self.aNew%s = %s ( '%s', '%s' )" % ( className, className,
arg1, arg2 )

and then "exec" it.  I expect/assume the classes to implement some
interface, but I have no idea what the names might be...

> You exec'd that piece of code in your current namespace, so it injected
new
> names there. Be very careful of the format of the strings you load from
the
> DB; exec is not as forgiving as the interpreter.

Yes, I was planning on it inserting new names in the namespace.  Does your
warning refer to syntax errors?

> >have been changed?  (reloading isn't THAT important, however loading
> >arbitrary classes whose names are unknown is a priority)
> Yes, and they'll clobber the existing names. However, existing instances
of
> Blarg will still have the old Blarg definition.

That's great, that's exactly what I would want to happen!

> If they're unknown names, you should probably look at passing exec a
> dictionary to use as a namespace, since that's easier to manage. You'll
> probably also want to use "apply" instead of exec'ing dynamically created
> code snippets to use the classes.

Why would I want to use apply?


--
----------------------------------------------------------------------
Olivier A. Dagenais - Carleton University - Computer Science III







More information about the Python-list mailing list