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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:59:41+00:00 2026-05-27T09:59:41+00:00

I want a decorator that would add the decorated function to list, like this

  • 0

I want a decorator that would add the decorated function to list, like this :

class My_Class(object):

    def __init__(self):
        self.list=[]

    @decorator
    def my_function(self)
       print 'Hi'

I expect my_function to be added to self.list, but I just can’t write this decorator. If I try to write it inside My_Class, then I would have to use @self.decorator, and self does not exist since we’re outside any function. And if I try to write it out of My_Class, then I can’t retrieve self from my_function.

I know quite similar questions exist, but they are overly complicated, and I’m just learning python and decorators.

  • 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-27T09:59:41+00:00Added an answer on May 27, 2026 at 9:59 am

    You can’t access self from the decorator, because the decorator is run at the time the function is defined, and at that time there are no instances of My_Class yet.

    It’s better to put the function list as a class attribute instead of an instance attribute. Then you can pass this list as a parameter to the decorator:

    def addToList(funcList):
        '''Decorator that adds the function to a given list'''
        def actual_decorator(f):
             funcList.append(f)
             return f
        return actual_decorator
    
    class MyClass(object):
        funcList = []
    
        @addToList(funcList)
        def some_function(self, name):
            print 'Hello,', name
    

    Now you can access MyClass.funcList to get the list of decorated functions.

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

Sidebar

Related Questions

Assuming I have a decorator and a wrapped function like this: def squared(method): def
I have code that looks like this: Class Parent: def someMethod(self): return 42 Class
I would like to implement a decorator that provides per-request caching to any method,
I want to write a decorator that acts differently depending on whether it is
I want to create a decorator that will allow me to return a raw
I would like to create a HtmlHelper function for a specific kind of dropdown
I've got the following problem: I need to write a decorator that would be
When you have a class car that implements IVehicle and you want to wrap
Is there any way of writing a decorator such that the following would work?
I have a DateTime TemplateEditor and I would like to add regex validation 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.