From: Ryan Kelly on
On Sun, 2010-01-31 at 15:25 -0500, Ray Holt wrote:
> Why am I getting the error that test is not defined. Thanks, Ray
> class SpecialFile:
> def __init__(self, fileName):
> self.__file = open(fileName, 'W')
> self.__file.write('***** Start Special File *****\n\n')
> def write(self, str):
> self.__file.write(str)
> def writelines(self, strList):
> self.__file.writelines(strList)
>
> def __del__(self): # Destructor method, __del__.
> print "entered __del__"
> self.close()
>
> def close(self): # Clean up method
> if self.__file:
> self.__file.write('\\n***** End Special File *****')
> self.__file.close()
> self.__file = None
>
> def test():
> f = SpecialFile('testfile')
> f.write('111111\n')
> f.close()
> test()
> Traceback (most recent call last):
> File "C:\Python26\Classes_and_OOP_Programing.py", line 145, in
> <module>
> test()
> NameError: name 'test' is not defined


Based on the indentation, it looks like you've defined "test" as a
method on the SpecialFile class. Try dedenting it to begin in the first
column.


Ryan

--
Ryan Kelly
http://www.rfk.id.au | This message is digitally signed. Please visit
ryan(a)rfk.id.au | http://www.rfk.id.au/ramblings/gpg/ for details