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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:05:17+00:00 2026-06-06T23:05:17+00:00

I’m trying to write a class method decorator that modifies its class’ state. I’m

  • 0

I’m trying to write a class method decorator that modifies its class’ state. I’m having troubles implementing it at the moment.

Side question: When does a decorator get called? Does it load when the class is instantiated or on during read time when the class read?

What I’m trying to do is this:

class ObjMeta(object):
    methods = []

    # This should be a decorator that magically updates the 'methods'
    # attribute (or list) of this class that's being read by the proxying
    # class below.
    def method_wrapper(method):
        @functools.wraps(method)
        def wrapper(*args, **kwargs):
            ObjMeta.methods.append(method.__name__)
            return method(*args, **kwargs)
        return wrapper

    # Our methods
    @method_wrapper
    def method1(self, *args):
        return args

    @method_wrapper
    def method2(self, *args):
        return args


class Obj(object):

    klass = None

    def __init__(self, object_class=ObjMeta):
        self.klass = object_class
        self._set_methods(object_class)

    # We dynamically load the method proxies that calls to our meta class
    # that actually contains the methods. It's actually dependent to the 
    # meta class' methods attribute that contains a list of names of its
    # existing methods. This is where I wanted it to be done automagically with
    # the help of decorators
    def _set_methods(self, object_class):
        for method_name in object_class:
            setattr(self, method_name, self._proxy_method(method_name))

    # Proxies the method that's being called to our meta class
    def _proxy_method(self, method_name):
        def wrapper(*fargs, **fkwargs):
            return getattr(self.klass(*fargs, **fkwargs), method_name)
        return wrapper()

I think it’s ugly to write a list of methods manually in the class so perhaps a decorator would fix this.

It’s for an open-source project I’m working that ports underscore.js to python. I understand that it says I should just use itertools or something. I’m just doing this just for the love of programming and learning. BTW, project is hosted here

Thanks!

  • 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-06-06T23:05:19+00:00Added an answer on June 6, 2026 at 11:05 pm

    There are a few things wrong here.

    Anything inside the inner wrapper is called when the method itself is called. Basically, you’re replacing the method with that function, which wraps the original. So, your code as it stands would add the method name to the list each time it is called, which probably isn’t what you want. Instead, that append should be at the method_wrapper level, ie outside of the inner wrapper. This is called when the method is defined, which happens the first time the module containing the class is imported.

    The second thing wrong is that you never actually call the method – you simply return it. Instead of return method you should be returning the value of calling the method with the supplied args – return method(*args, **kwargs).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to select an H1 element which is the second-child in its group
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.