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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:59:55+00:00 2026-05-15T17:59:55+00:00

I want to instrument a class via a decorator, to add some instance variables

  • 0

I want to instrument a class via a decorator, to add some instance variables that are specified by the author of the class.

I started with the following code, but this just adds class variables and I want instance variables (those that are normally ‘declared’ in __ init __)

What is a pythonic way to do this, while allowing the class author control over what they put in __ init __?

def add_list_attributes(klass):
    for attribute in klass.list_attributes:
        setattr(klass, attribute, [])

    return klass

@add_list_attributes
class Person(object):

    list_attributes = [
        'phone_numbers'
    ]

    def __init__(self):
        pass

p1 = Person()
p1.phone_numbers.append('01234')

print p1.phone_numbers
  • 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-15T17:59:56+00:00Added an answer on May 15, 2026 at 5:59 pm

    You can create a custom __new__ method:

    def add_list_attributes(klass):
        def new(cls, *args, **kwargs):
            result = super(cls, cls).__new__(cls)
            for attribute in klass.list_attributes:
                setattr(result, attribute, [])
            return result
        klass.__new__ = staticmethod(new)
        return klass
    
    @add_list_attributes
    class Person(object):
        list_attributes = [
            'phone_numbers'
        ]
        def __init__(self):
            pass
    
    p1 = Person()
    p2 = Person()
    p1.phone_numbers.append('01234')
    
    print p1.phone_numbers, p2.phone_numbers
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently implementing some code that should, for each method of a class, run
I need to programmatically instrument the lines of code of a method so that
I want to be able to instrument Python applications so that I know: Page
I want to lazy load of @Lob properties. First ,i use javassist to instrument
Want to code a key pad for an calculator. What I want to make
I have the following ul setup: <div id=sidebar> <ul id=themenu class=sf-menu sf-vertical> <li> <a
I have a model like this: public class Instrument { public string Id {
I have a Flex 3 app which I want to instrument to report errors
My iOS app is an musical instrument and I want to make a record
I want to create a simple visualization tool that would allow to represent my

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.