Tuesday, February 9, 2016

Undecoratable

Decorators are one of Python's bigger success stories, and many programmers' first experience with higher-order programming. Most practiced and prolific Python programmers will find themselves making good use of them regularly.

But every feature has its limits, and here's a new one to try on for size:

>>> @x().y()
  File "<stdin>", line 1
    @x().y()
        ^
SyntaxError: invalid syntax

That's right, decoration is not an arbitrary Python expression. It doesn't matter what x and y were, or even if they were defined. You can't follow a function call with a dot. @x() works fine, @x.y() would work fine, too. But @x().y(), that's only for mad Pythonists who would take things TOO FAR.

Decorator invocations, defined at the top of the Python grammar, can only be followed by class definitions and function definitions.

Well, now we know, and now we can all say we've been there

-- Mahmoud
http://sedimental.org/
https://github.com/mahmoud
https://twitter.com/mhashemi

18 comments:

  1. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,
    Python Training in Chennai|Python Training Institutes in Chennai

    ReplyDelete
  2. I had a chance to learn Undecoratable from your post, thanks for this valuable post.
    Best DOT NET Training in Chennai|.net training

    ReplyDelete
  3. Thanks for sharing. I read your post and gained more knowledge about ur blog. JAVA training institute in Chennai | DotNet training institute in Chennai.

    ReplyDelete
  4. Your post is awesome. Thanks for sharing such a valuable post and keep updating...
    Oracle Training Institute in Chennai

    ReplyDelete
  5. This blog is amazing.Its very helpful to us,thanks for sharing such a wonderful article.
    Oracle Training Institute in Chennai | Oracle Training Institute in Velachery

    ReplyDelete
  6. Your post is nice.Now a day python technology is high demand,thank you for sharing such a great post.
    Dot Net Training Institute in Chennai | Best Dot Net Training Institute in Chennai

    ReplyDelete
  7. Your post good and informative. Thank you for sharing. keep rocking always. Linux Training Institute in Chennai | Oracle Training Institute in Chennai.

    ReplyDelete
  8. Nice post, Thanks for sharing this information with us.
    IT Job Training in Chennai

    ReplyDelete
  9. Your blog was awesome and very interesting to read. keep updating such a wonderful blog. Dot Net Training in Chennai | Java Training in Chennai

    ReplyDelete
  10. Really, these quotes are the holistic approach toward mindfulness. In fact, all of your posts are. Proudly saying I’m getting fruitfulness out of what you write and share. Thank you so much
    Best institute chennai

    ReplyDelete