class - python className not defined NameError -
i have class need instantiate in order call method contains. when access class works fine when run terminal says :
file "myclass.py", line 5, in <module> class myclass: file "myclass.py", line 23, in todict td=myclass() nameerror: name 'myclass' not defined
pasting code:
class myclass: def convert(self,fl): xpd={} # process stuff return xpd if __name__=="__main__": source=sys.argv[1] td=myclass() needed_stuff=td.convert(source) print needed_stuff
looking @ indentation of code (and edit history of question), suspect if __name__ == "__main__"
block inside of class definition. cause error, code within if
run part of class being created, before class been bound name.
here's simpler example of error:
class foo(object): foo = foo() # raises nameerror because name foo isn't bound yet
if format code way looks in question (that is, if
unindented @ top level), should work correctly.
Comments
Post a Comment