The problem is that your if __name__ == "__main__"
block is inside of your class definition. This will cause an error, as the code within the if
will be run as part of the class being created, before the class been bound to a name.
Here's a simpler example of this error:
class Foo(object):
foo = Foo() # raises NameError because the name Foo isn't bound yet
If you format your code like this (that is, with the if
unindented at the top level), it should work correctly:
class MyClass:
def convert(self, fl):
xpD = {}
# process some stuff
return xpD
if __name__ == "__main__":
source = sys.argv[1]
td = MyClass()
needed_stuff = td.convert(source)
print(needed_stuff)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…