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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:38:38+00:00 2026-05-27T00:38:38+00:00

I have some python module, which has a class ModuleClass and I can’t modify

  • 0

I have some python module, which has a class ModuleClass and I can’t modify that class.

Now, I want to be able to proxify the method calls and add certain logging features. I assume this should be done via forwarding object and the corresponding proxy (following Effective Java, Item 16).

The python pseudocode I came up with follows.

(Sorry, I’m really bad at python and I would appreciate if you could point out errors here).

# This is what I've got in my module and this code cannot be changed.
class ModuleClass(object):
    def method1(self):
        # Some implementation
        pass()
    def method2(self):
        # Some implementation
        pass()

# Simple forwarding proxy to avoid the situation described in Effective Java, I16

# However, in Java this class would usually be extending the interface, not
# inheriting 'ModuleClass' (I'm confused and don't know how to do the same
# in python).

class ForwardingModuleClass(ModuleClass):
    # 'proxifiedObject' is 
    def __init__(self, proxifiedObject):
        self.proxifiedObject = proxifiedObject

    # Overriding the first method
    def method1(self):
        return self.proxifiedObject.method1()

    # Same for method2...

class LoggingModuleClass(ForwardingModuleClass):
    # 'classThatActuallyDoesStuff' should be an instance of 'ModuleClass'.
    def __init__(self, classThatActuallyDoesStuff):
        # Sorry for my bad knowledge of python syntax, but
        # I assume I can initialize the superclass here using
        # the supplied 'ModuleClass' instance.
        super(classThatActuallyDoesStuff)

    # Overriding the first method.
    def method1(self):
        print("Yay! This 'method1' really logs something!")
        return super.method1()

    # Overriding the second method.
    def method2(self):
        print("Yay!!!! This 'method2' also does something cool!")
        return super.method2()

Now, I guess, properly written, this would work and I would have the logging proxy for my initial ModuleClass.

If there are errors or if this is not pythonish, please point it out.

Also, I suspect this can easily be done using decorators, but, unfortunately, I can’t figure the appropriate way and I have no idea what might happen if ModuleClass already has some method decorators.

Could you help me here too?

  • 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-27T00:38:39+00:00Added an answer on May 27, 2026 at 12:38 am

    If you really want a wrapper, then just write it, with no subclassing and intermediate classes.

    class LoggingFoo(object):
        def __init__(self, *args, **kwargs):
            self.obj = Foo(*args, **kwargs)
    
        def method1(self):
            # ...
            return self.obj.method1()
    
        def method2(self):
            # ...
            return self.obj.method2()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Karmic Koala which has Python 2.6 installed by default. However I can't
I have a python module which generates large data files which I want to
I have installed some python packages which I am able to access using IDLE
I have installed Sphinx in order to document some Python modules and class I'm
I have some Python code that works correctly when I use python.exe to run
I have some old python code that uses the pywin32 extensions. Starting out with
I have created some python code which creates an object in a loop, and
I have some code in a python string that contains extraneous empty lines. I
I have some global variables in a Python script. Some functions in that script
I've done some Python but have just now starting to use Ruby I could

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.