Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 257411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:05:41+00:00 2026-05-11T22:05:41+00:00

I recently started experimenting with Python decorators (and higher-order functions) because it looked like

  • 0

I recently started experimenting with Python decorators (and higher-order functions) because it looked like they might make my Django unit tests more concise. e.g., instead of writing:

def visit1():
    login()
    do_stuff()
    logout()

I could instead do

@handle_login
def visit1():
    do_stuff()

However, after some experimenting, I have found that decorators are not as simple as I had hoped. First, I was confused by the different decorator syntax I found in different examples, until I learned that decorators behave very differently when they take arguments. Then I tried decorating a method, and eventually learned that it wasn’t working because I first have to turn my decorator into a descriptor by adding a __get__ method. During this whole process I’ve ended up confused more than a few times and still find that debugging this “decorated” code is more complicated than it normally is for Python. I’m now re-evaluating whether I really need decorators in my code, since my initial motivation was to save a bit of typing, not because there was anything that really required higher-order functions.

So my question is: should decorators be used liberally or sparingly? Is it ever more Pythonic to avoid using them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:05:41+00:00Added an answer on May 11, 2026 at 10:05 pm

    Decorators are fine in their place and definitely not to be avoided — when appropriate;-). I see your question as meaning essentially “OK so when are they appropriate”?

    Adding some prefix and/or postfix code around some but not all methods of some classes is a good example. Were it all methods, a class decorator to wrap all methods would be better than repeating @thisonetoo endlessly;-). If it’s once in a blue moon then it’s not worth refactoring out to wrappers (decorators or otherwise). In the middle, there’s a large ground where decorators are quite suitable indeed.

    It boils down to one of the golden rules of programming — DRY, for Don’t Repeat Yourself. When you see your code becoming repetitious, you should refactor the repetition out — and decorators are an excellent tool for that, although they’re far from the only one (auxiliary methods and functions, custom metaclasses, generators and other iterators, context managers… many of the features we added to Python over the last few years can best be thought of as DRY-helpers, easier and smoother ways to factor out this or that frequent form of repetition!).

    If there’s no repetition, there’s no real call for refactoring, hence (in particular) no real need for decorators — in such cases, YAGNI (Y’Ain’t Gonna Need It) can trump DRY;-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 187k
  • Answers 187k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It sounds like you have a dual-core CPU and your… May 12, 2026 at 5:29 pm
  • Editorial Team
    Editorial Team added an answer Use GREATEST() E.g.: SELECT GREATEST(2,1); Note: Whenever if any single… May 12, 2026 at 5:29 pm
  • Editorial Team
    Editorial Team added an answer I assume you are talking about the server-side code of… May 12, 2026 at 5:28 pm

Related Questions

I have recently started experimenting with Django for some web applications in my spare
We have a very strange error occurring at a developer site which we are
I'm in the business of making website and applications that are not mission critical
I'm using XML Serialization heavily in a web service (the contracts pass complex types

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.