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

  • Home
  • SEARCH
  • 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 6158093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:53:50+00:00 2026-05-23T20:53:50+00:00

I want to implement function which takes as argument any object and trackes changes

  • 0

I want to implement function which takes as argument any object and trackes changes of value for specific attribute. Than saves old value of attribute in old_name attribute.

For example:

class MyObject(object):
     attr_one = None
     attr_two = 1

Lets name my magic function magic_function()

Sot than i can do like this:

obj = MyObject()
obj = magic_function(obj)
obj.attr_one = 'new value'
obj.attr_two = 2

and it saves old values so i can get like this

print obj.old_attr_one
None
print obj.attr_one 'new value'

and

print obj.old_attr_two
1
print obj.attr_two
2

Something like this.. I wonder how can i do this by not touching the class of instance?

  • 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-23T20:53:51+00:00Added an answer on May 23, 2026 at 8:53 pm

    This is a start:

    class MagicWrapper(object):
        def __init__(self, wrapped):
            self._wrapped = wrapped
    
        def __getattr__(self, attr):
            return getattr(self._wrapped, attr)
    
        def __setattr__(self, attr, val):
            if attr == '_wrapped':
                super(MagicWrapper, self).__setattr__('_wrapped', val)
            else:
                setattr(self._wrapped, 'old_' + attr, getattr(self._wrapped, attr))
                setattr(self._wrapped, attr, val)
    
    
    class MyObject(object):
        def __init__(self):
            self.attr_one = None
            self.attr_two = 1
    
    obj = MyObject()
    obj = MagicWrapper(obj)
    obj.attr_one = 'new value'
    obj.attr_two = 2
    
    print obj.old_attr_one
    print obj.attr_one
    print obj.old_attr_two
    print obj.attr_two
    

    This isn’t bullet-proof when you’re trying to wrap weird objects (very little in Python is), but it should work for “normal” classes. You could write a lot more code to get a little bit closer to fully cloning the behaviour of the wrapped object, but it’s probably impossible to do perfectly. The main thing to be aware of here is that many special methods will not be redirected to the wrapped object.

    If you want to do this without wrapping obj in some way, it’s going to get messy. Here’s an option:

    def add_old_setattr_to_class(cls):
        def __setattr__(self, attr, val):
            super_setattr = super(self.__class__, self).__setattr__
            if attr.startswith('old_'):
                super_setattr(attr, val)
            else:
                super_setattr('old_' + attr, getattr(self, attr))
                super_setattr(attr, val)
        cls.__setattr__ = __setattr__
    
    
    class MyObject(object):
        def __init__(self):
            self.attr_one = None
            self.attr_two = 1
    
    obj = MyObject()
    add_old_setattr_to_class(obj.__class__)
    obj.attr_one = 'new value'
    obj.attr_two = 2
    
    print obj.old_attr_one
    print obj.attr_one
    print obj.old_attr_two
    print obj.attr_two
    

    Note that this is extremely invasive if you’re using it on externally provided objects. It globally modifies the class of the object you’re applying the magic to, not just that one instance. This is because like several other special methods, __setattr__ is not looked up in the instance’s attribute dictionary; the lookup skips straight to the class, so there’s no way to just override __setattr__ on the instance. I would characterise this sort of code as a bizarre hack if I encountered it in the wild (it’s “nifty cleverness” if I write it myself, of course 😉 ).

    This version may or may not play nicely with objects that already play tricks with __setattr__ and __getattr__/__getattribute__. If you end up modifying the same class several times, I think this still works, but you end up with an ever-increasing number of wrapped __setattr__ definitions. You should probably try to avoid that; maybe by setting a “secret flag” on the class and checking for it in add_old_setattr_to_class before modifying cls. You should probably also use a more-unlikely prefix than just old_, since you’re essentially trying to create a whole separate namespace.

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

Sidebar

Related Questions

For simplicity, let's say I want to do implement a function which takes two
in C++, when you define a function which takes one argument, you have to
I'm trying to implement a function that takes a System.Drawing.Bitmap object and renders it
I want to implement a function in the base class but I also want
I'm using R5RS Scheme and I just want to implement a function that returns
I want to implement a scroll function.. So the default of scroll is disabled.
I want to implement greatest integer function. [The greatest integer function is a quite
i want to implement next previous functionality using below function. - (void)motionEnded:(UIEventSubtype)mt withEvent:(UIEvent *)event
i want to return wchar_t frm a function. How can i implement it wchar_t
I'm attempting to override and implement my own TabExpansion. In the function I want

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.