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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:15:06+00:00 2026-05-30T06:15:06+00:00

I have some code like class EventHandler: def handle(self, event): pass def wrap_handler(handler): def

  • 0

I have some code like

class EventHandler:
    def handle(self, event):
        pass

def wrap_handler(handler):
    def log_event(proc, e):
        print e
        proc(e)
    handler.handle = lambda e: log_event(handler.handle, e)

handler = EventHandler()
wrap_handler(handler)
handler.handle('event')

that will end up with infinite recursion. While changing wrap_handler to

def wrap_handler(handler):
    def log_event(proc, e):
        print e
        proc(e)
    # handler.handle = lambda e: log_event(handler.handle, e)
    handle_func = handler.handle
    handler.handle = lambda e: log_event(handle_func, e)

the program will become OK. Why is that? And could anyone tell me more common ways to wrap functions?

  • 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-30T06:15:07+00:00Added an answer on May 30, 2026 at 6:15 am

    It ends in an infinite recursion as when lambda e: log_event(handler.handle, e) is called, handler.handle is already the lambda expression. log_event would call the lambda and the lambda would call log_event, etc..

    To fix this, just save the current method in the local-scope, this also does not need the additional lambda-expression.

    class EventHandler:
        def handle(self, event):
            pass
    
    def wrap_handler(handler):
        proc = handler.handle
        def log_event(e):
            print e
            proc(e)
        handler.handle = log_event
    
    handler = EventHandler()
    wrap_handler(handler)
    handler.handle('event')
    

    You could also use a decorator.

    def logging(function):
        def wrapper(*args, **kwargs):
            print "Calling %s with:" % function.__name__, args, kwargs
            return function(*args, **kwargs)
        return wrapper
    
    class EventHandler:
        @ logging
        def handle(self, event):
            pass
    
        def __repr__(self):
            return "EventHandler instance"
    
    handler = EventHandler()
    handler.handle('event')
    

    C:\Users\niklas\Desktop>foo.py
    Calling handle with: (EventHandler instance, ‘event’) {}

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

Sidebar

Related Questions

I have some code like this: doDatabaseFetch { ... @synchronized(self) { ... } }
I have some code like this: case class FunctionCommand[A](function: Function1[Array[A], Unit]) class MyClass(commands: List[FunctionCommand[_]])
Say I have some code like namespace Portal { public class Author { public
I have some code like that: class MainApplication { protected static string _since; protected
I have some code like this: public class EffectValues : IEnumerable<object> { public object
I have some code like this: var content = document.getElementById('myDivId'); function MyFunction() { alert(content.style.height);
In PHP, say that you have some code like this: $infrastructure = mt_rand(0,100); if
In my application there is language detection. Languages have some identification code like en
I have some code that looks like: static const std::string and( AND ); This
I have some code that looks like this. There is also an autoincrement field

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.