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

  • Home
  • SEARCH
  • 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 69061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:30:51+00:00 2026-05-10T19:30:51+00:00

I am instantiating a class A (which I am importing from somebody else, so

  • 0

I am instantiating a class A (which I am importing from somebody else, so I can’t modify it) into my class X.

Is there a way I can intercept or wrap calls to methods in A? I.e., in the code below can I call

x.a.p1() 

and get the output

X.pre A.p1 X.post 

Many TIA!

class A:     # in my real application, this is an imported class     # that I cannot modify     def p1(self): print 'A.p1'  class X:     def __init__(self):         self.a=A()     def pre(self): print 'X.pre'     def post(self): print 'X.post'  x=X() x.a.p1() 
  • 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. 2026-05-10T19:30:52+00:00Added an answer on May 10, 2026 at 7:30 pm

    Here is the solution I and my colleagues came up with:

    from types import MethodType  class PrePostCaller:     def __init__(self, other):         self.other = other      def pre(self): print 'pre'     def post(self): print 'post'      def __getattr__(self, name):         if hasattr(self.other, name):             func = getattr(self.other, name)             return lambda *args, **kwargs: self._wrap(func, args, kwargs)         raise AttributeError(name)      def _wrap(self, func, args, kwargs):         self.pre()         if type(func) == MethodType:             result = func( *args, **kwargs)         else:             result = func(self.other, *args, **kwargs)         self.post()         return result  #Examples of use class Foo:     def stuff(self):         print 'stuff'  a = PrePostCaller(Foo()) a.stuff()  a = PrePostCaller([1,2,3]) print a.count() 

    Gives:

    pre stuff post pre post 0 

    So when creating an instance of your object, wrap it with the PrePostCaller object. After that you continue using the object as if it was an instance of the wrapped object. With this solution you can do the wrapping on a per instance basis.

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

Sidebar

Related Questions

Are there any circumstances in which Class.getDeclaringClass could give a different result from Class.getEnclosingClass
Is there a way to call functions within a class upon instantiation of that
I have the following abstract class which CAN NOT be changed. public abstract class
In my project, say I am instantiating a class named SubClass , which extends
I have a method foo in class C which either calls foo_1 or foo_2.
Is it possible, when instantiating an object, to pass-in a class which the object
I am trying to wrap a C++ library in Objective-C classes for importing into
I hit an interesting python bug today in which instantiating a class repeatedly appears
I found this code online where the person is instantiating a class which has
I have a JS class inside which I am instantiating a goog.net.XhrIo object. Now

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.