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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:30:34+00:00 2026-05-18T04:30:34+00:00

I have a list of Spam objects: class Spam: def update(self): print(‘updating spam!’) some

  • 0

I have a list of Spam objects:

class Spam:
    def update(self):
        print('updating spam!')

some of them might be SpamLite objects:

class SpamLite(Spam):
    def update(self):
        print('this spam is lite!')
        Spam.update(self)

I would like to be able to take an arbitrary object from the list, and add something to it’s update method, something like:

def poison(spam):
    tmp = spam.update 
    def newUpdate(self):
        print 'this spam has been poisoned!'
        tmp(self)
    spam.update = newUpdate

I want spam.update() to now either print:

this spam has been poisoned!
updating spam!

or

this spam has been poisoned!
this spam is lite!
updating spam!

depending on whether it was a SpamLite or just a Spam.

But that doesn’t work, because spam.update() won’t pass in the self argument automatically, and because if tmp leaves scope or changes then it won’t call the old update. Is there a way I can do this?

  • 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-18T04:30:35+00:00Added an answer on May 18, 2026 at 4:30 am
    def poison(spam):
        tmp = spam.update
        def newUpdate():
            print 'this spam has been poisoned!'
            tmp()
        spam.update = newUpdate
    

    Full Script:

    class Spam:
        def update(self):
            print('updating spam!')
    
    class SpamLite(Spam):
        def update(self):
            print('this spam is lite!')
            Spam.update(self)
    
    def poison(spam):
        tmp = spam.update # it is a bound method that doesn't take any arguments
        def newUpdate():
            print 'this spam has been poisoned!'
            tmp()
        spam.update = newUpdate
    
    
    from operator import methodcaller    
    L = [Spam(), SpamLite()]
    map(methodcaller('update'), L)
    map(poison, L)
    print "*"*79
    map(methodcaller('update'), L)
    

    Output:

    updating spam!
    this spam is lite!
    updating spam!
    *******************************************************************************
    this spam has been poisoned!
    updating spam!
    this spam has been poisoned!
    this spam is lite!
    updating spam!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have list of rows that user select and i want to delete them,
class Foo { Bar b; List<Foo> Neighbours; } class Bar { Spam s; List<Bar>
Basically, I have a list like: [START, 'foo', 'bar', 'spam', eggs', END] and the
I have List<Product[]> and I need to join them into one Product[] .
I have List<Custom> where Custom is class Custom { public int id; public String
I have list of some data items. Each has a method GetStyle which returns
I have a bad (spam) words list for spam protection filters. I want to
I have some a list comprehension in Python in which each iteration can throw
I have a list of sites that represent spam links: List<String> bannedSites = [spam1.com,
What exactly are the Python scoping rules? If I have some code: code1 class

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.