lundi 26 septembre 2022

Fallback function in python

is there some kind of fallback function (Solidity analogy) in python? A fallback function is always called when no corresponding method is found in a class.

For example I have class A with no methods, but only a fallback function:

class A:
   def fallback(self, *args, **kwargs):
       # Somehow get the function name from kwargs
       print(f"Function {kwargs['function_name']} doesn't exist}"

So, when I call a nonexistent function I should get "Function non_existent doesn't exist", instead of AttributeError Exception.

a = A()
a.non_existent()

Aucun commentaire:

Enregistrer un commentaire