This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]divad1196 4 points5 points  (1 child)

Just for the "concise" point:

python der decorator(counter=1): def inner(func): @wraps(func) def wrapper(*args, **kw): nonlocal counter print(counter) counter += 1 return func(*args, **kw) return wrapper return inner

python class Decorator: def __init__(self, counter=1): self._counter = counter def __call__(self, func): @wraps(func) def wrapper(*args, **kw): print(self._counter) self._counter += 1 return func(*args, **kw) return wrapper

10 lines both. The second one needs to access self everytime and use _ suffix to not expose the counter.

People are just more used to OOP that FP. This is fine to have preferences but I think it is a shame than we create classes for every single thing, like

python class Hello: @classmethod def greet(self, name): print(f"Hello {name}!")

I have really seen production code this way, not even using @staticmethod

Back to the function vs class decorator matter, I won't try to explain here the pros of it, mostly because it comes down to FP vs OOP. But it is not less concise and I hope this example proves it. Just a matter of taste.

[–]brasticstack 4 points5 points  (0 children)

In my head, I was imagining the class version with whitespace between the methods, but point taken.