jeudi 14 janvier 2021

Is there a function argument decorator like in python?

I have some functions in python that I need to verify if some of the arguments is None and raise an Exception otherwise. Currently my solution is:

def notnone(arg):
  if arg == None:
    raise Exception()
  return arg

def example_function(..., a, ...):
  a = notnone(a)
  ...

In python, is there a more elegant solution? As elegant as the decorators are for functions? I'm imagining something like follows (although I know it won't work):

def example_function(..., @notnone a, ...):
  ...

Aucun commentaire:

Enregistrer un commentaire