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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:10:31+00:00 2026-06-02T05:10:31+00:00

I would like to map a method over a list of objects instantiating different

  • 0

I would like to map a method over a list of objects instantiating different classes. All the objects inherit from the same base class and define a method of the desired name.

To make it clearer consider the following code:

class A:
    def __init__(self, x):
        self.x = x

    def foo (self):
        return self.x

class B(A):
    def foo (self):
        return self.x+1

class C(A):
    def foo (self):
        return self.x-1

Now consider a list of objects instantiating the classes B and C. I would like to do something like that:

result = []
for obj in [B(1), C(1)]:
    result.append(obj.foo())

How would you proceed to map the method foo on each element of the list? Is it at all possible? The best I could come up with is something like that:

map(A.foo, [B(1), C(1)])

but clearly it doesn’t return my desired result. How can I specify the method related to the object?

I hope I made myself clear.

NB: I work primarily with Python2.7, but I would equally be interested in solutions valid for “newer” versions.

  • 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-06-02T05:10:32+00:00Added an answer on June 2, 2026 at 5:10 am
    >>> map(lambda x: x.foo(), [B(1), C(1)])
    >>> [2, 0]
    

    The lambda function will take each object in the list and call foo() on that object. Thus the resulting list will have the results returned by the corresponding object’s foo().

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

Sidebar

Related Questions

We would like to map a single table on two classes with NHibernate. The
I have a Map<X, Y> and List<X> , I would like to extract the
I would like to map foreign characters, especially Turkish characters, to their Latin-1 equivalent
I would like a Map implementation in which i could add listeners for put()
I would like to map the key combination CTRL+Left_Arrow to M-b in SecureCRT for
I would like to map a sequence of integers to a sequence of expression
I would like to map one triangle inside an OpenCV Mat to another one,
I have this table structure and would like to map it using Fluent Hibernate
I have an XML file that I would like to map some attributes of
I plan to process large compressed files and I would like to memory map

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.