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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:18:31+00:00 2026-06-14T18:18:31+00:00

I have a python hash that contains a mapping of function name to function.

  • 0

I have a python hash that contains a mapping of function name to function. I want to modify each hash entry to call the associated function BUT then to also call a final custom function. It’s to act somewhat like an exit hook.

def original():
    print "original work"

becomes

def replacement():
    original()
    print "notify somebody..."

My issue is I think I’m getting my scoping etc., wrong as the output of the following code is not as expected. Maybe if I could ask is there a better way to do this? I want to stick with modifying the original cb has as it third party code and less places I change the better.

#!/usr/bin/python 

def a():
    print "a"

def b():
    print "b"

def c():
    print "c"


orig_fxn_cb = dict()

" basic name to function callback hash "
orig_fxn_cb['a'] = a
orig_fxn_cb['b'] = b
orig_fxn_cb['c'] = c

" for each call back routine in the hash append a final action to it "
def appendFxn(fxn_cb):
    appended_fxn_cb_new = dict()
    for i in orig_fxn_cb.keys():
        cb = fxn_cb[i]
        def fxn_tail():
            cb()
            print cb.__name__, "tail" 

        appended_fxn_cb_new[i] = fxn_tail
        appended_fxn_cb_new[i]()
    return appended_fxn_cb_new

" make up a modified callback hash "
xxx = appendFxn(orig_fxn_cb)
print xxx

for i in xxx:
    print xxx[i]()
  • 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-06-14T18:18:32+00:00Added an answer on June 14, 2026 at 6:18 pm

    Are you after:

    from functools import wraps
    
    def notifier(f):
        @wraps(f)
        def wrapped(*args, **kwargs)
            res = f(*args, **kwargs)
            print "notify somebody..."
            return res
        return wrapped
    
    @notifier
    def original():
        print "original work"
    
    # or: original = notifier(original)
    
    original()
    
    original work
    notify somebody...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python program that takes the md5 & sha1 hash values of
Lets say that I have only DHT (distributed hash table) implemented (in Python), and
I have a Python set that contains objects with __hash__ and __eq__ methods in
I have a need for a high-performance string hashing function in python that produces
I have Python nested list that I'm trying to organize and eventually count number
Suppose you have a MD5 hash encoded in base64. Then each character needs only
I have an class definition with a __hash__ function that uses the object properties
I have noticed that in Python some of the fundamental types have two types
I have a python dictionary with a thousand items. Each item is, itself, a
Question: I've profiled my Python program to death, and there is one function that

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.