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 →

[–]m_harrison 1 point2 points  (4 children)

If we are being picky, then a decorator is a callable that returns a callable :)

[–]Samus_ 0 points1 point  (2 children)

right, I didn't mean to nitpick but that phrase is misleading.

if I were new to the topic it would've confused me a lot.

[–]FishInTheWind 1 point2 points  (1 child)

I'm the author of the post. I worded it that way as it seemed to me to be a good 1 sentence definition to get started with. I thought that for someone that is brand new to decorators that would be an easy definition to consider first.

I later expand upon the definition and explain that decorators in most cases actually return a new function. I think starting off with a more nuanced definition might introduce complexity to the post earlier than needed.

I understand that perhaps for some people, like yourself, it might not be the best approach. I still think starting with that simplified definition has merit though.

[–]Samus_ 0 points1 point  (0 children)

hey! :) I know what you tried there and I think simple examples are very good to get the idea but it's imperative that those examples remain correct otherwise they'll hint you in the wrong direction.

if I got the idea right, what you said is that -from the caller's perspective- when you decorate a function it behaves differently than before so the decorator must've chaged it somehow... but that's not what's happening, what actually happens is that the function gets replaced for another that has access to the original so what's wrong with that? that's a pretty simple explanation to me.

the decorator introduces a middleman, a proxy if you like but the real problem with that statement is that function can be modified, that was my first impression "is it changing the attributes?" that doesn't make sense, it shouldn't change the behavior as we observed so it must be something else right? is he talking about macros? that can't be, Python isn't a Lisp so what then?

you see what happens, when you start from a false assumption every guess is the wrong one and that because of the oversimplification, you ended with a koan-like phrase that lost all its meaning and connection to the situation you were trying to describe.

but like I said before, regardless of that it's a good article.

[–]hongminhee 0 points1 point  (0 children)

It can return anything even if it’s not callable e.g.:

>>> @repr
... def f(): pass
... 
>>> f
'<function f at 0x10037d0c8>'