Is there a way to automatically pass self to a lambda function in a class? I know I can pass self in the __init__ function but then I'm redefining token_functions for every instance of Parser. token_functions never changes so this seems quite inefficient. Is there a better way to do this?
class Parser:
token_functions = {r'\n': lambda: Parser.new_line(self)}
def __init__(self):
self.line_number = 0
Parser.token_functions[r'\n']()
def new_line(self):
self.line_number += 1
print(self.line_number)
Parser()
Parserclass and redefinetoken_functionsfor each instance.NameError.Parseris not defined yet.__init__, new_line, Or, lambda.