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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:06:24+00:00 2026-05-11T07:06:24+00:00

Lets say I have a library function that I cannot change that produces an

  • 0

Lets say I have a library function that I cannot change that produces an object of class A, and I have created a class B that inherits from A.

What is the most straightforward way of using the library function to produce an object of class B?

edit- I was asked in a comment for more detail, so here goes:

PyTables is a package that handles hierarchical datasets in python. The bit I use most is its ability to manage data that is partially on disk. It provides an ‘Array’ type which only comes with extended slicing, but I need to select arbitrary rows. Numpy offers this capability – you can select by providing a boolean array of the same length as the array you are selecting from. Therefore, I wanted to subclass Array to add this new functionality.

In a more abstract sense this is a problem I have considered before. The usual solution is as has already been suggested- Have a constructor for B that takes an A and additional arguments, and then pulls out the relevant bits of A to insert into B. As it seemed like a fairly basic problem, I asked to question to see if there were any standard solutions I wasn’t aware of.

  • 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-11T07:06:25+00:00Added an answer on May 11, 2026 at 7:06 am

    Since the library function returns an A, you can’t make it return a B without changing it.

    One thing you can do is write a function to take the fields of the A instance and copy them over into a new B instance:

    class A: # defined by the library     def __init__(self, field):         self.field = field  class B(A): # your fancy new class     def __init__(self, field, field2):         self.field = field         self.field2 = field2 # B has some fancy extra stuff  def b_from_a(a_instance, field2):     '''Given an instance of A, return a new instance of B.'''     return B(a_instance.field, field2)   a = A('spam') # this could be your A instance from the library b = b_from_a(a, 'ham') # make a new B which has the data from a  print b.field, b.field2 # prints 'spam ham' 

    Edit: depending on your situation, composition instead of inheritance could be a good bet; that is your B class could just contain an instance of A instead of inheriting:

    class B2: # doesn't have to inherit from A     def __init__(self, a, field2):         self._a = a # using composition instead         self.field2 = field2      @property     def field(self): # pass accesses to a         return self._a.field     # could provide setter, deleter, etc  a = A('spam') b = B2(a, 'ham')  print b.field, b.field2 # prints 'spam ham' 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 110k
  • Answers 110k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sorry mate but you'll have to make a variable out… May 11, 2026 at 9:32 pm
  • Editorial Team
    Editorial Team added an answer We have been using MS test tools since VS2005, and… May 11, 2026 at 9:32 pm
  • Editorial Team
    Editorial Team added an answer Analysis Services and Views have the fundamental difference that they… May 11, 2026 at 9:32 pm

Related Questions

Let's say that you have to implement some functionality that is not trivial (it
EDIT: I suppose I should clarify, in case it matters. I am on a
I recently wrote a small number-crunching program that basically loops over an N-dimensional grid
I've been working with jQuery for a pair of weeks and I've noticed it

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.