Allow reserved key words as methods in CPython

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:

  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 would error with a syntax error (cf GH-pyodide#769)

Read more here:

Content Attribution

This content was originally published by rth at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: