News for hackers. Stuff that matters.
Re:Why Python is Important for You by craigyk on Saturday February 11, 2012 @10:58PM (#3581059)
if Python had better function syntax and anonynous functions, decorators wouldn't even be a 'thing'
Re:Why Python is Important for You by j-kidd on Sunday February 12, 2012 @01:34AM (#3581277)
Can you elaborate on that? The more I use Python, the more I find it to be a well thought out language with a very consistent feel, allowing me to write code without thinking. Well, except when it comes to decorators. How would you propose to improve the syntax?
Re:Why Python is Important for You by irahul on Sunday February 12, 2012 @06:56AM (#3581607)
> if Python had better function syntax and anonynous functions, decorators wouldn't even be a 'thing'

Better function syntax? What's wrong with python's function syntax and how does it relate to decorators?

Python does have anonymous functions, though they are limited: You can have only 1 expression, you can't have statements, and you can't have local variables. The reasoning is lambdas should be used for small operations; for everything else, named functions should be used.

The reasoning behind lambdas is arguable, but it still doesn't explain how it affects decorators.

Consider:

    def memoize(fn):
        from functools import wraps
        @wraps(fn)
        def memoized_fn(*args):
            if not memoized_fn.cache.has_key(args):
                memoized_fn.cache[args] = fn(*args)
            return memoized_fn.cache[args]
        memoized_fn.cache = {}
        return memoized_fn

    @memoize
    def fib(n):
        if n < 2: return n
        return fib(n-1) + fib(n-2)

    print fib(100)

What would a better function syntax and anonymous functions do so that memoize decorator won't be a thing?
Re:Why Python is Important for You by maxerickson on Sunday February 12, 2012 @07:50AM (#3581685)
Less restricted anonymous functions would allow stuff like this:

    fib=memoize(function...)
So the @ shortcut for:

    def fib():
        ...
    fib=memoize(fib)
wouldn't be necessary to 'clean up' the decoration of the function.
Re:Why Python is Important for You by irahul on Sunday February 12, 2012 @08:01AM (#3581707)
Even if it were possible, I would stick with

    @memoize
    def fib:
        pass
fib = memoize(function ...) has unnecessary nesting, and @memoize is cleaner IMO.

I am curious - do you find @memoize confusing? Personally for me and anyone I have talked to, @memoize is vanilla - the confusing part is the decorator itself, especially one that takes arguments; that coupled with the fact that classes(which override __call__) and functions are both used as decorators.

Re:Why Python is Important for You by maxerickson on Sunday February 12, 2012 @08:09AM (#3581724)
No, I don't find it confusing. But I read craigyk as mostly talking about the special decorator syntax, not function decoration in general.