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 1112561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:43:53+00:00 2026-05-17T02:43:53+00:00

Compared to decorators applied to a function, it’s not easy to understand the decorators

  • 0

Compared to decorators applied to a function, it’s not easy to understand the decorators applied to a class.

@foo
class Bar(object):
    def __init__(self, x):
        self.x = x
    def spam(self):
        statements

What’s the use case of decorators to a class? How to use it?

  • 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-17T02:43:53+00:00Added an answer on May 17, 2026 at 2:43 am

    It replaces the vast majority of classic good uses for custom metaclasses in a much simpler way.

    Think about it this way: nothing that’s directly in the class body can refer to the class object, because the class object doesn’t exist until well after the body’s done running (it’s the metaclass’s job to create the class object — usually type‘s, for all classes without a custom metaclass).

    But, the code in the class decorator runs after the class object is created (indeed, with the class object as an argument!) and so can perfectly well refer to that class object (and usually needs to do so).

    For example, consider:

    def enum(cls):
      names = getattr(cls, 'names', None)
      if names is None:
        raise TypeError('%r must have a class field `names` to be an `enum`!',
                        cls.__name__)
      for i, n in enumerate(names):
        setattr(cls, n, i)
      return cls
    
    @enum
    class Color(object):
      names = 'red green blue'.split()
    

    and now you can refer to Color.red, Color.green, &c, rather than to 0, 1, etc. (Of course you normally would add even more functionality to make “an enum“, but here I’m just showing the simple way to put such functionality addition in a class decorator, rather than needing a custom metaclass!-)

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

Sidebar

Related Questions

What is the memory and performance usage compared to creating a object with only
Not sure if these two can be compared, to bear with me. But what
Is WPF easy to learn when compared to other languages?. Or should the Approach
Concerning Trie from Wikipedia: [Compared to HashTable] Tries support ordered iteration I am not
Why whole structure can not be compared in C yet it can be copied?
I'm not sure if Vim makes me more productive compared to other editors/ide's like
Does someone can help me to understand WHERE to collocate Gwt compared to (for
I heard threading is not very efficient in Python (compared to other languages). Is
compared with physical machine, the VM is much more easy to change the machine
I know that add is faster as compared to mul function. I want to

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.