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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:34:19+00:00 2026-05-14T01:34:19+00:00

The title isn’t very clear but I’ll try to explain. Having this class: class

  • 0

The title isn’t very clear but I’ll try to explain.

Having this class:

class Wrapped(object):
    def method_a(self):
        # do some operations
        return n

    def method_b(self):
        # also do some operations
        return n

I want to have a class that performs the same way as this one:

class Wrapper(object):
    def __init__(self):
        self.ws = [Wrapped(1),Wrapped(2),Wrapped(3)]

    def method_a(self):
        results=[Wrapped.method_a(w) for w in self.ws]
        sum_ = sum(results,0.0)
        average = sum_/len(self.ws)
        return average

    def method_b(self):
        results=[Wrapped.method_b(w) for w in self.ws]
        sum_ = sum(results,0.0)
        average = sum_/len(self.ws)
        return average

Obviously this is not the actual problem at hand (it is not only two methods), and this code is also incomplete (only included the minimum to explain the problem).

So, what i am looking for is a way to obtain this behavior. Meaning, whichever method is called in the wrapper class, call that method for all the Wrapped class objects and return the average of their results.

Can it be done? How?

Thanks in advance.

Edit
Thanks for the answers… after seeing them the solution it all seems obvious 🙂 I have chosen Alex Martelli’s answer because it explained the solution well. The other answers where also useful, that’s why I also voted them up.

  • 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-14T01:34:20+00:00Added an answer on May 14, 2026 at 1:34 am

    While quite doable, it’s just a little bit tricky because the getting of a method (or other attribute) and the calling thereof are separate operations. Here’s a solution:

    class Wrapper(object):
        def __init__(self):
            self.ws = [Wrapped(1),Wrapped(2),Wrapped(3)]
    
        def __getattr__(self, n):
            meth = getattr(Wrapped, n)
            def caller():
                results = [meth(w) for w in self.ws]
                sum_ = sum(results,0.0)
                average = sum_/len(self.ws)
                return average
            return caller
    

    It’s a bit simplistic (assumes no change in self.ws between the getting and the calling — one could of course “snapshot” self.ws at the moment of the getting, if that’s the desired semantics; doesn’t use @functools.wraps so doesn’t preserve the docstring &c if those need preserving), but should be mostly workable for your purposes.

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

Sidebar

Related Questions

I know the title isn't very elaborate, but I have tried this multiple times
I’m sorry if the title isn’t clear, but this is a weird problem. I
Well, i know the title isn't very specific, but i couldn't find a better
Hope that title isn't too cryptic. I have an array with a DATETIME object
Sorry if the title isn't descriptive enough. I want to be able to extract
I know this is going to not be the same across all languages, but
I've got an Inspector window whose title (sometimes) isn't ideal for reporting to visually
A lot of people put extremely useless and annoying stuff in their <title> tags
i wan't to know if there is a way to decrease the Run Speed
I have several pages that access my data-base directly and get values from a

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.