3 ms·
Overall it's a good article, but I don't fully understand his complaint about decorators, and I think he may not understand them. @foo and @foo() intentionally
by andrewflnr 15y ago
Overall it's a good article, but I don't fully understand his complaint about decorators, and I think he may not understand them. @foo and @foo() intentionally mean very different things. I don't see how you could "add a parameter to a previously parameter-less decorator" without drastically changing the meaning of the whole thing.
They may be tricky to wrap your head around, but there's nothing too surprising about decorators.
- irahul 15y ago> I think he may not understand them. I won't think so. He codes a lot, in general, and in Python. I have read some of his posts and code, and he has a deep understanding of Python and programming in general. His complain was about @foo and @foo() requires different decorator implementations. If @foo by default meant @foo(), that makes introducing parameters at a later time a bit more straightforward. I am not arguing about it being a valid expectation. I am just explaining what I think he meant.
- MostAwesomeDude 15y agoIf I have the following code: @auth_required def some_view(): pass And later I decide to change that decorator to be parameterized: @auth_required("basic") def some_view(): pass I have now created a massive amount of pain for myself if I decide to define auth_required() such that it defaults its argument to "basic", unless I force people to call it as @auth_required(), because of the differing callable signatures. Some people feel @auth_required() is ugly. That's all.
- the_mitsuhiko 15y ago> Some people feel @auth_required() is ugly. That's all. The biggest issue here is backwards compatibility. Nowadays I just make a separate decorator that accepts arguments and keep the old one around unchanged. I learned my lesson :)
- d0m 15y agoYou may also use a simple wrapper to create decorators which would automatically define @decorator and @decorator().
- the_mitsuhiko 15y agoI could also aim with a bazooka at my foot. Been there done that, lesson learned :-)
- andrewflnr 15y agoApparently there's something pretty big here I'm not getting. If you add a parameter to auth_required, of course you're causing a huge amount of pain because you're totally changing what auth_required means. It's going from being directly applied to some_view to generating a function that is applied to some_view. If every decorator was automatically called, every decorator would have to return a function. Among other consequences, you wouldn't be able to use property as a decorator. Even if it's inconvenient, python's behavior here is still not surprising. It's perfectly consistent, and more general than automatically adding parentheses.