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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:03:51+00:00 2026-05-29T07:03:51+00:00

I have two classes (let’s call them Working and ReturnStatement) which I can’t modify,

  • 0

I have two classes (let’s call them Working and ReturnStatement) which I can’t modify, but I want to extend both of them with logging. The trick is that the Working’s method returns a ReturnStatement object, so the new MutantWorking object also returns ReturnStatement unless I can cast it to MutantReturnStatement. Saying with code:

# these classes can't be changed
class ReturnStatement(object):
    def act(self):
        print "I'm a ReturnStatement."

class Working(object):
    def do(self):
        print "I am Working."
        return ReturnStatement()

# these classes should wrap the original ones
class MutantReturnStatement(ReturnStatement):
    def act(self):
        print "I'm wrapping ReturnStatement."
        return ReturnStatement().act()

class MutantWorking(Working):
    def do(self):
        print "I am wrapping Working."
        # !!! this is not working, I'd need that casting working !!!
        return (MutantReturnStatement) Working().do()

rs = MutantWorking().do() #I can use MutantWorking just like Working
print "--" # just to separate output
rs.act() #this must be MutantReturnState.act(), I need the overloaded method

The expected result:
I am wrapping Working.
I am Working.
—
I’m wrapping ReturnStatement.
I’m a ReturnStatement.

Is it possible to solve the problem? I’m also curious if the problem can be solved in PHP, too. Unless I get a working solution I can’t accept the answer, so please write working code to get accepted.

  • 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-29T07:03:52+00:00Added an answer on May 29, 2026 at 7:03 am

    There is no casting as the other answers already explained. You can make subclasses or make modified new types with the extra functionality using decorators.

    Here’s a complete example (credit to How to make a chain of function decorators?). You do not need to modify your original classes. In my example the original class is called Working.

    # decorator for logging
    def logging(func):
        def wrapper(*args, **kwargs):
            print func.__name__, args, kwargs
            res = func(*args, **kwargs)
            return res
        return wrapper
    
    # this is some example class you do not want to/can not modify
    class Working:
        def Do(c):
            print("I am working")
        def pr(c,printit):   # other example method
            print(printit)
        def bla(c):          # other example method
            c.pr("saybla")
    
    # this is how to make a new class with some methods logged:
    class MutantWorking(Working):
        pr=logging(Working.pr)
        bla=logging(Working.bla)
        Do=logging(Working.Do)
    
    h=MutantWorking()
    h.bla()
    h.pr("Working")                                                  
    h.Do()
    

    this will print

    h.bla()
    bla (<__main__.MutantWorking instance at 0xb776b78c>,) {}
    pr (<__main__.MutantWorking instance at 0xb776b78c>, 'saybla') {}
    saybla
    
    pr (<__main__.MutantWorking instance at 0xb776b78c>, 'Working') {}
    Working
    
    Do (<__main__.MutantWorking instance at 0xb776b78c>,) {}
    I am working
    

    In addition, I would like to understand why you can not modify a class. Did you try? Because, as an alternative to making a subclass, if you feel dynamic you can almost always modify an old class in place:

    Working.Do=logging(Working.Do)
    ReturnStatement.Act=logging(ReturnStatement.Act)
    

    Update: Apply logging to all methods of a class

    As you now specifically asked for this. You can loop over all members and apply logging to them all. But you need to define a rule for what kind of members to modify. The example below excludes any method with __ in its name .

    import types
    def hasmethod(obj, name):
        return hasattr(obj, name) and type(getattr(obj, name)) == types.MethodType
    
    def loggify(theclass):
      for x in filter(lambda x:"__" not in x, dir(theclass)):
         if hasmethod(theclass,x):
            print(x)
            setattr(theclass,x,logging(getattr(theclass,x)))
      return theclass
    

    With this all you have to do to make a new logged version of a class is:

    @loggify
    class loggedWorker(Working): pass
    

    Or modify an existing class in place:

    loggify(Working)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes that I am testing (let's call them ClassA and ClassB).
I have two classes (let's call them A and B ), both inheriting from
I have two classes which represent a special kind of numeric value. Let's call
Is it possible in C++ to have two classes, let's call them A and
I have two classes, let's call them parent and child, and both need to
Let's say you have two classes that extend UserControl . Each of the controls
I have two classes in a many-to-many relationship, but only one of them have
I have two classes, one which is hardware-dependent and one which is not (let's
here is my problem with a Java program: I have two classes (let's call
Let's say I have two classes Foo and Bar, and I want to make

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.