Converting an instance to a subclass?

D-Man dsh8290 at rit.edu
Fri Feb 16 14:42:33 EST 2001


On Fri, Feb 16, 2001 at 01:49:26PM -0500, Tom Bridgman wrote:
[snip] 
| and I have a number of methods that return me instances or lists of
| instances of this class.  These are part of my general library of
| classes for this project.
| 
| However, I need to write a utility which will define methods that are
| only needed by the utility.  I really don't want these methods to be
| 'permanent' members of the class so I define them in a subclass as part
| of the utility.
| 
| Class B(A):
|    def Cleanup(self,x,y,z):
|        ...
| 

This is a good way to add functionality that isn't always needed --
you can use instances of 'A' when it's not needed, and instances of
'B' when it is.

| Is there a way I can cast the instances of class A into instances of
| class B so I can use the additional methods?  I can't find anything
| about it in "Programming Python" but then I'm not quite sure where to
| look either.

Since you use the word 'cast', I take it you have some C, C++ or Java
experience.  Those lanugages are statically typed, so if you declared
having one type, but really have another, you must explicitly tell the
compiler with a cast.  Python is dynamically typed, so if you really
have a "different" type (it can't really be different since you didn't
declare it in the first place, but) you have it and python is happy.

What I suspect is happening is that you are creating instances of A
when you really want B.

>>> class A : pass
...
>>> class B( A ) : pass
...
>>> obj1 = A()
>>> obj2 = B()
>>> obj1.__class__
<class __main__.A at 007A8934>
>>> obj2.__class__
<class __main__.B at 007AA9A4>
>>>

In this example, obj2 is an instance of class "B".  Since "B" is a
subclass of "A", obj2 is also an instance of class "A".  You can use
obj2 whereever an instance of A or B is expected.  On the other hand,
obj1 is an instance of class "A", and won't work where an instance of
class B is expected.  There is no way automatically to turn obj1 into
an instance of class B.  There are a couple of ways to work around
though.

1)  modify the factory functions to return instances of "B" instead.
    Since it is a subclass, all existing code will work.

2)  Make a conversion that will create a new instance of class "B" and
    init all data members based on an instance of class "A"

3)  Play some obscure tricks using python dynamism to add those
    functions to obj1 even though they don't belong there


#1 may be the easiest, but then you might as well just put the
functions in class "A" and only use them when you need to.

#2 could be done in B's __init__ function :

   class B( A ) :
       def __init__( self , instance_of_A ) :
           self.value = instance_of_A.value
           # ...

   Then when you want an instance of B, you can create it from the
   given instance of A.

#3 isn't good since it is obscure and will be difficult to maintain.

HTH,
-D





More information about the Python-list mailing list