Hello, I am playing with class. Below is the code snippet: #!/usr/bin/python 2 3 class test_class: 4 #import gzip 5 def __init__(self,file): 6 self.file = file 7 def open_file(self): 8 try: 9 print "file: %s" % self.file 10 self.xml_file = gzip.GzipFile(self.file,'r') 11 except: 12 print "an exception has occured" 13 for line in self.xml_file: 14 print "line: %s" % line 15 self.xml_file.close() 16 17 18 if __name__ == '__main__': 19 import gzip 20 import sys 21 t = test_class( sys.argv[1] ) 22 t.open_file()
My question are: 1. Why do I need to use "import gzip" on main section to get it the script to work? I would assume you need the import of gzip in the class section. 2. What is the proper way of using module in a class you are creating? -- http://mail.python.org/mailman/listinfo/python-list