tags:

views:

385

answers:

3

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.

+6  A: 

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"
Kiv
A: 

Monkeypatch the library?

For example,

import other_library
other_library.function_or_class_to_replace = new_function

Poof, it returns whatever you want it to return.

Monkeypatch A.new to return an instance of B?

After you call obj = A(), change the result so obj.class = B?

Andrew Dalke
+1  A: 

This can be done if the initializer of the subclass can handle it, or you write an explicit upgrader. Here is an example:

class A(object):
    def __init__(self):
        self.x = 1

class B(A):
    def __init__(self):
        super(B, self).__init__()
        self._init_B()
    def _init_B(self):
        self.x += 1

a = A()
b = a
b.__class__ = B
b._init_B()

assert b.x == 2
ironfroggy