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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:56:42+00:00 2026-05-12T13:56:42+00:00

I want to create a class that wraps another class so that when a

  • 0

I want to create a class that wraps another class so that when a function is run through the wrapper class a pre and post function is run as well. I want the wrapper class to work with any class without modification.

For example if i have this class.

class Simple(object):
    def one(self):
        print "one"

    def two(self,two):
        print "two" + two

    def three(self):
        print "three"

I could use it like this…

number = Simple()
number.one()
number.two("2")

I have so far written this wrapper class…

class Wrapper(object):
    def __init__(self,wrapped_class):
        self.wrapped_class = wrapped_class()

    def __getattr__(self,attr):
        return self.wrapped_class.__getattribute__(attr)

    def pre():
        print "pre"

    def post():
        print "post"

Which I can call like this…

number = Wrapper(Simple)
number.one()
number.two("2")

Which can be used the same as above apart from changing the first line.

What I want to happen is when calling a function through the wrapper class the pre function in the wrapper class gets called then the desired function in the wrapped class then the post function. I want to be able to do this without changing the wrapped class and also without changing the way the functions are called, only changing the syntax of how the instance of the class is created. eg number = Simple() vs number = Wrapper(Simple)

  • 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-12T13:56:42+00:00Added an answer on May 12, 2026 at 1:56 pm

    You’re almost there, you just need to do some introspection inside __getattr__, returning a new wrapped function when the original attribute is callable:

    class Wrapper(object):
        def __init__(self,wrapped_class):
            self.wrapped_class = wrapped_class()
    
        def __getattr__(self,attr):
            orig_attr = self.wrapped_class.__getattribute__(attr)
            if callable(orig_attr):
                def hooked(*args, **kwargs):
                    self.pre()
                    result = orig_attr(*args, **kwargs)
                    # prevent wrapped_class from becoming unwrapped
                    if result == self.wrapped_class:
                        return self
                    self.post()
                    return result
                return hooked
            else:
                return orig_attr
    
        def pre(self):
            print ">> pre"
    
        def post(self):
            print "<< post"
    

    Now with this code:

    number = Wrapper(Simple)
    
    print "\nCalling wrapped 'one':"
    number.one()
    
    print "\nCalling wrapped 'two':"
    number.two("2")
    

    The result is:

    Calling wrapped 'one':
    >> pre
    one
    << post
    
    Calling wrapped 'two':
    >> pre
    two2
    << post
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a wrapper class so that all queries should not be
I want to create a class that initializes a timer which will be used
I want to create whois class like that public class DomainInfo { public string
I want to create a Javascript class/object that allow me to have various method:
If I create a class that extends UserControl and want to set a default
I want to create a substr method in C++ in a string class that
I have a signed class library that I want to create assemblies for via
I want to create a template class (let's call it Foo ) that only
I have create my own NSOpenGLView class, right now the data that i want
I want to create a class which wraps a list of structures. I have

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.