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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:22:17+00:00 2026-05-14T14:22:17+00:00

Goal: Make a decorator which can modify the scope that it is used in.

  • 0

Goal: Make a decorator which can modify the scope that it is used in.

If it worked:

class Blah(): # or perhaps class Blah(ParentClassWhichMakesThisPossible)

    def one(self):
        pass

    @decorated
    def two(self):
        pass

>>> Blah.decorated
["two"]

Why? I essentially want to write classes which can maintain specific dictionaries of methods, so that I can retrieve lists of available methods of different types on a per class basis. errr…..

I want to do this:

class RuleClass(ParentClass):
    @rule
    def blah(self):
        pass

    @rule
    def kapow(self):
        pass

    def shazam(self):

class OtherRuleClass(ParentClass):
    @rule
    def foo(self):
        pass

    def bar(self):
        pass

>>> RuleClass.rules.keys()
["blah", "kapow"]
>>> OtherRuleClass.rules.keys()
["foo"]
  • 1 1 Answer
  • 1 View
  • 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-14T14:22:17+00:00Added an answer on May 14, 2026 at 2:22 pm

    You can do what you want with a class decorator (in Python 2.6) or a metaclass. The class decorator version:

    def rule(f):
        f.rule = True
        return f
    
    def getRules(cls):
        cls.rules = {}
        for attr, value in cls.__dict__.iteritems():
            if getattr(value, 'rule', False):
                cls.rules[attr] = value
        return cls
    
    @getRules
    class RuleClass:
        @rule
        def foo(self):
            pass
    

    The metaclass version would be:

    def rule(f):
        f.rule = True
        return f
    
    class RuleType(type):
        def __init__(self, name, bases, attrs):
            self.rules = {}
            for attr, value in attrs.iteritems():
                if getattr(value, 'rule', False):
                    self.rules[attr] = value
            super(RuleType, self).__init__(name, bases, attrs)
    
    class RuleBase(object):
        __metaclass__ = RuleType
    
    class RuleClass(RuleBase):
        @rule
        def foo(self):
            pass
    

    Notice that neither of these do what you ask for (modify the calling namespace) because it’s fragile, hard and often impossible. Instead they both post-process the class — through the class decorator or the metaclass’s __init__ method — by inspecting all the attributes and filling the rules attribute. The difference between the two is that the metaclass solution works in Python 2.5 and earlier (down to 2.2), and that the metaclass is inherited. With the decorator, subclasses have to each apply the decorator individually (if they want to set the rules attribute.)

    Both solutions do not take inheritance into account — they don’t look at the parent class when looking for methods marked as rules, nor do they look at the parent class rules attribute. It’s not hard to extend either to do that, if that’s what you want.

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

Sidebar

Related Questions

Goal is to make a dialog that appears on menu_key pressed, but it keeps
My goal is to make a layout that is 200% width and height, with
Goal: Make it possible to decorate class methods. When a class method gets decorated,
My goal is to make a modern webapp that uses location hash for navigation
i have a problem with this class. the goal is to make the main
On this page: www.incfilms.net/services, my goal is to make that Advertising and Consulting picture
My goal is to make a flash/as3 program that would pull multiple sets of
My goal is to make an HTML page that is similar to a photo
My goal is to make a Button that has two Content values. Imagine a
My goal is to make a DIV that presents the latest four news links

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.