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

5 comments:

  1. I like your blog, I read this blog please update more content on hacking, further check it once at python online training

    ReplyDelete
  2. Mobiastuce
    Get your hands on unlimited pc & mac torrent applications. You can easily download and install these premium applications on your macbook mobiastuce.com

    ReplyDelete
  3. InstantPcApps
    It's a hub for free mac, pc, android and ios software. You can get any kind of game, software and mobile application free of cost. Razer Surround Pro Crack

    ReplyDelete