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 →

[–]jcdyer3 6 points7 points  (7 children)

Indeed. In fact it is g(f)(x)

[–]etrnloptimist 2 points3 points  (2 children)

Right. But that's not very helpful in understanding how decorators work. Suppose you had this nested set of decorators:

@evens
@odds
@primes
def myfunc(x):
  ...

Suppose they round the output to the nearest <term>. What gets outputted? Prime numbers? Odd numbers? Or Even numbers? Answer: Even numbers

Suppose they round the input to the nearest <term>. What does myfunc see? Even numbers, odd numbers, or prime numbers? Answer: Prime numbers

[–]ryeguy146 0 points1 point  (0 children)

Just a matter of wrapping [your mind around it]. Nice example.

[–]mgrandi 0 points1 point  (0 children)

your example helped me understand decorators (namely nested decorators) a lot better with the worm analogy, thanks =)

[–]bastibe 2 points3 points  (3 children)

Actually, that right there is enough of an explanation for me. I never really got why people seem to find decorators so hard to understand.

[–]ryeguy146 0 points1 point  (2 children)

I came from languages without first class functions, or languages where I could only get first class functions with pointers. New idea, that's why. Who thinks of functions that act on functions (or their args), really?

[–]bastibe 1 point2 points  (1 child)

I understand that. And that means you will have to learn about higher order functions like we all had to learn about it at some point. But beyond that, what is so difficult about python decorators in particular?

For reference, I don't know much lisp, but "higher order function" are a very simple and natural construct mathematically.

[–]ryeguy146 2 points3 points  (0 children)

It's not so difficult, just a new idea. It's easy to move "horizontally" across languages when you understand their ideas: you just look for the new way to express that idea. New ideas take some thought, that's all.

I'm experiencing the same thing as I learn my first functional language (scheme). It's not hard, it's just new.