import in a class

TP Tribulations at Paralleles.invalid
Wed Nov 26 12:30:53 EST 2008


Hi everybody,

Here is a file "test_import_scope.py":
##########
class a():
   import re
   def __init__( self ):
      if re.search( "to", "toto" ):
         self.se = "ok!"
   def print_se( self ):
      print self.se
a().print_se()
##########

When python executes this file, we obtain an error:

$ python test_import_scope.py
[...]
NameError: global name 're' is not defined

Why?

When the re module is imported in the __init__ function or out of the class
(at the top of the file), it works obviously perfectly.

Thanks in advance,

Julien

-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)



More information about the Python-list mailing list