tags:

views:

60

answers:

2

Hello all. I am a newbe to Python. I have tried to create a class, named ic0File.

Here is what I get when I use it (Python 3.1)

>>> import sys
>>> sys.path.append('/remote/us01home15/ldagan/python/')
>>> import ic0File
>>> a=ic0File.ic0File('as_client/nohpp.ic0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ic0File.py", line 7, in __init__
    print ("".join(self.lines))
NameError: global name 'infile' is not defined

The class code is:

class ic0File:
    def __init__(self,filename):
        self.infile = open(filename, 'r')
        import sys
        import re
        self.lines=self.infile.readlines() #reading the lines
        print ("".join(self.lines)

Thanks,

A: 

To reload a module (e.g. if you have modified the code), use reload(). In your case:

reload( ic0file )

In Python 3, reload was moved to the imp library:

import imp
imp.reload( ic0file )
katrielalex
A: 

As other users have pointed out, the code that actually raised that exception would be helpful, but my guess is that you're trying to access the infile attribute of a ic0File as if it was actually a variable.

You've probably written something like this:

self.lines = infile.readlines() #reading the lines

instead of:

self.lines = self.infile.readlines() #reading the lines

in one of the methods of ic0File. Unlike in other languages, object attributes don't become local variables in said object's methods.

Honza