decorators

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Henry

    #1

    decorators

    I must be very thick. I keep reading about what decorators are and I
    still don't have a good feel about it. See, for example:



    and:



    What exactly do I use decorators for?

  • Paul McGuire

    #2
    Re: decorators

    "John Henry" <john106henry@h otmail.comwrote in message
    news:1163018240 .383103.183930@ b28g2000cwb.goo glegroups.com.. .
    >I must be very thick. I keep reading about what decorators are and I
    still don't have a good feel about it. See, for example:
    >

    >
    and:
    >

    >
    What exactly do I use decorators for?
    >
    John -

    See if any of these examples give you some ideas on how you might use a
    decorator: http://wiki.python.org/moin/PythonDecoratorLibrary

    -- Paul


    Comment

    • Carsten Haese

      #3
      Re: decorators

      On Wed, 2006-11-08 at 12:37 -0800, John Henry wrote:
      I must be very thick. I keep reading about what decorators are and I
      still don't have a good feel about it. See, for example:
      >

      >
      and:
      >

      >
      What exactly do I use decorators for?
      Decorators are a tool for executing common code when a function is
      defined and/or when it's called. This reduces code duplication by
      allowing you to factor out commonly performed steps.

      If you find yourself writing different functions that share a lot of
      setup/teardown code, or if you find yourself doing a lot of repetitive
      housekeeping for each function you're defining, decorators eliminate
      this duplication.

      You've already been given a bunch of different examples, so I won't bore
      you with more contrived examples. If none of the examples make you say
      "wow, I could use this" and if you don't find yourself writing
      repetitive setup/teardown/housekeeping code, you can probably live quite
      comfortably without using decorators.

      -Carsten


      Comment

      • Paddy

        #4
        Re: decorators


        John Henry wrote:
        I must be very thick. I keep reading about what decorators are and I
        still don't have a good feel about it. See, for example:
        >

        >
        and:
        >

        >
        What exactly do I use decorators for?
        Here's my learning experience on decorators:
        Prompted by a presentation were someone said Python doesn't have full closures (noodle.odp), I looked at their example given and thought fir...

        especially:
        It appears to be straight forward to initialize function attributes by a decorator (the setup function). The new function factory looks a lo...


        Comment

        • Paul McGuire

          #5
          Re: decorators

          "Carsten Haese" <carsten@uniqsy s.comwrote in message
          news:mailman.19 38.1163022175.1 1739.python-list@python.org ...
          If none of the examples make you say
          "wow, I could use this" and if you don't find yourself writing
          repetitive setup/teardown/housekeeping code, you can probably live quite
          comfortably without using decorators.
          >
          Amen! Don't force yourself to learn decorators if you don't need them at
          the moment. Wait until you start repeating the same code (lock/unlock,
          dbready/dbcommit, logstart/logend, etc.) around multiple functions, then go
          back and try applying a decorator to simplify the job. Having a real use
          for them will make it much easier to "get".

          I will say, though, that I found the memoizing decorator to be especially
          intriguing and eye-opening, and didn't realize I needed it until I saw it
          laid out in front of me.

          -- Paul


          Comment

          Working...