mardi 18 avril 2017

how can i change a class method name with decorators (python)?

I want to change the method name from CamelCase to snake_case with a decorator, i tried this but it didn't work:

def snakecase(newname):
    def decorator(method):
          method.__name__ = newname
          return method
    return decorator

class Product:
    def __init__(self, name, last_name, age):
    self.name = name
    self._last_name = last_name
    self.age = age

@snakecase('increase_years')
def IncreaseYears(self, n_years):
    self.age += n_years

Anybody knows how to do it? Any help will be appreciated..






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire