Another form of dynamic import
Terry Reedy
tjreedy at udel.edu
Wed Mar 25 14:53:49 EDT 2009
Marco Nawijn wrote:
> In short I would like to know if somebody knows if it is possible to
> re-execute a statement that raised an exception?
In short, no.
> As an example, look at the following statement
>
>>> aPoint = gp_Pnt(1.0, 0.0, 0.0) # Oops, this will raise a NameError, since
> # gp_Pnt class
> is unknown
>
> NameError: name 'gp_Pnt' is not defined
Either make sure names are defined before you use them (easy) or provide
alternative code in the except clause.
If you actually have the name written literally in the code, you should
simply import it first, so I presume you have a more complicated use
case where the name to use is dynamically defined in a string. If so,
do something like
aPoint = MyImporter(class_name)(1.0, 0.0, 0.0)
where MyImporter.__new__ imports and returns the class
Terry Jan Reedy
More information about the Python-list
mailing list