It looks like Python has a list of reserved key words that cannot be used as method names. For instance,
class A:
def finally(self):
return 0
returns a SyntaxError: invalid syntax
. There is a way around it with getattr/setattr
,
class A:
pass
setattr(A, 'finally', lambda self: 0)
a = A()
print(getattr(a, "finally")())
works fine. However, a.finally()
still produces a SyntaxError: invalid syntax
.
Is there a way to avoid it? More specifically, are there some settings when compiling CPython 3.8 from sources (or a code patch) that would allow avoiding this error?
Note that the same error happens in PyPy 3.
The context is that in Pyodide, that builds CPython to WebAssembly, one can pass Javascripts objects to Python. And because of the present limitation, currently, Python code like Promise.new(...).then(...).finally(...)
would error with a syntax error (cf GH-pyodide#769)
I'm not aware of any such flags but I imagine it would be a Herculean feat seeing as how the reserved keywords are baked into the very grammar of Python. You'd have to make some structural changes to allow keywords to be used as method names. It could be easy, but one would have to have knowledge about Python's grammar and how it translates to C. You can start by looking into the Tools section, particularly the peg_generator and parser projects.
Hopefully someone more knowledgeable than I am can give a more satisfying answer.