Design pattern question

Thomas Jensen spam at ob_scure.dk
Wed Aug 21 21:57:57 CEST 2002


Hi,

Karl Schmid wrote:

> Now I would like to instantiate the abstract class, but to obtain an 
> instance of either the Cap3Assembler or PhrapAssembler class depending on 
> the assembler_type variable.
> 
> 
>>>>assembler = SequenceAssembler(assembler_type)
>>>
> 
> Is this possible? 
> 

This code prints 1, 1 and 0:

--- code begin ---

   class SequenceAssembler:

     def __init__(self,assembler_type):

       if assembler_type == 'cap3':
         self.__class__ = Cap3Assembler
       elif assembler_type == 'phrap':
         self.__class__ = PhrapAssembler
       else:
         raise Exception('illigal assembler_type %s' % repr(assembler_type))

     def save_sequences():
       pass

   class Cap3Assembler(SequenceAssembler):

     def run_assembler(self):
       print 'Cap3Assembler.run()'

   class PhrapAssembler(SequenceAssembler):

     def run_assembler(self):
       print 'PhrapAssembler.run()'


   sa = SequenceAssembler('cap3')
   print isinstance(sa, SequenceAssembler)
   print isinstance(sa, Cap3Assembler)
   print isinstance(sa, PhrapAssembler)

--- code end ---

I don't think this is recommended practice, but I was unable to find any 
documents saying this is forbidden or will go away in a future release. 
(Maybe I didn't look hard enough though ;-)

-- 
Best Regards
Thomas Jensen
(remove underscore in email address to mail me)




More information about the Python-list mailing list