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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:57:43+00:00 2026-05-22T12:57:43+00:00

For specific debugging purposes I’d like to wrap the del function of an arbitrary

  • 0

For specific debugging purposes I’d like to wrap the del function of an arbitrary object to perform extra tasks like write the last value of the object to a file.

Ideally I want to write
monkey(x)
and it should mean that the final value of x is printed when x is deleted

Now I figured that del is a class method. So the following is a start:

class Test:
    def __str__(self):
        return "Test"

def p(self):
    print(str(self))

def monkey(x):
    x.__class__.__del__=p

a=Test()
monkey(a)
del a

However if I want to monkey specific objects only I suppose I need to dynamically rewrite their class to a new one?! Moreover I need to do this anyway, since I cannot access del of built-in types?

Anyone knows how to implement that?

  • 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-22T12:57:43+00:00Added an answer on May 22, 2026 at 12:57 pm

    While special ‘double underscore’ methods like __del__, __str__, __repr__, etc. can be monkey-patched on the instance level, they’ll just be ignored, unless they are called directly (e.g., if you take Omnifarious’s answer: del a won’t print a thing, but a.__del__() would).

    If you still want to monkey patch a single instance a of class A at runtime, the solution is to dynamically create a class A1 which is derived from A, and then change a‘s class to the newly-created A1. Yes, this is possible, and a will behave as if nothing has changed – except that now it includes your monkey patched method.

    Here’s a solution based on a generic function I wrote for another question:
    Python method resolution mystery

    def override(p, methods):
        oldType = type(p)
        newType = type(oldType.__name__ + "_Override", (oldType,), methods)
        p.__class__ = newType
    
    
    class Test(object):
        def __str__(self):
            return "Test"
    
    def p(self):
        print(str(self))
    
    def monkey(x):
        override(x, {"__del__": p})
    
    a=Test()
    b=Test()
    monkey(a)
    print "Deleting a:"
    del a
    print "Deleting b:"
    del b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a specific DateTime value, how do I display relative time, like: 2 hours
In the sake of debugging purposes, can I get the line number in C
Before I ask this, do note: I want this for debugging purposes. I know
While debugging python application, I normally use pdb / ipdb's set_trace() function to programmatically
XP-specific answers preferred, but others are welcome too.
This is a very specific question regarding MySQL as implemented in WordPress . I'm
I think this is specific to IE 6.0 but... In JavaScript I add a
I need to disable specific keys (Ctrl and Backspace) in Internet Explorer 6. Is
Anyone familiar with specific security issues in the current version of DotNetNuke? (I've already
Is there a specific pattern that developers generally follow? I never really gave it

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.