Question about using from/import from with a class

Peter Otten __peter__ at web.de
Tue Jul 27 14:01:50 EDT 2004


John Taylor wrote:

> I have a class that has this code in it:
> 
> def __init__(self,Site):
>     self.Site = Site
>     import_cmd = "from regexpr_%d import *" % ( int(self.Site.id) )
>     exec( import_cmd )
>     print AGENT # fails, NameError: global name 'AGENT' is not defined
> 
> 
> The regexpr_1.py file, for example, has the line:
> AGENT="Mozilla"

It may be bad style but it _does_ work here. I would verify that

[faked:]

>>> import regexpr_1
>>> regexpr_1.AGENT
'Mozilla'

in the interpreter doesn't also raise the NameError. If so, check
int(self.Site.id).

Peter





More information about the Python-list mailing list