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

I have the following abstract class which CAN NOT be changed. public abstract class
I hit an interesting python bug today in which instantiating a class repeatedly appears
We're seeing a crash when instantiating an instance of the System.Xml.Serialization.XmlSerializer class in a
As you can see below, in the constructor I'm instantiating a validation object so
Ok guys I basically have a class which takes in 3 strings through the
I have a case class which takes a list of functions: case class A(q:Double,
I was reading an article in which the author had implemented an Entity class
Is there a (practical) way to by-pass the normal (virtual) constructor calling order? Example:
I have a template class, inside which i have a normal function. But i
I am instantiating a class like this. myObj = (myObj) Class.forName(fully qualified class name

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.