fix NameError: global name 'self' is not defined in main function