lots of similar classes?

Mike C. Fletcher mcfletch at rogers.com
Mon Jan 20 10:10:04 EST 2003


With Python 2.2.x, this will work:

class X(object):
    def __init__( self, name='blah', y=2,z=3,m=4):
        ...

class Y( X ):
    def __init__( self, name='haha', *args, **named ):
        super( Y, self).__init__( name, *args, **named )

The disadvantage is that automatic documentation systems (or you, 
reading the code) won't be able to immediately see what the arguments 
are for the initialiser (looking at Y, you can't see that it takes 
arguments 'y','z', and 'm'). That's possibly acceptable if you've got a 
large class of objects with equivalent semantics where your docs tell 
the user about them up-front.

Another 2.2.x approach is to use properties with defaults and automatic 
__init__ methods, so that your sub-classes merely define those 
properties they wish to override and the automatic initialiser takes 
care of everything else.  With this approach your automatic 
documentation systems will be able to document the arguments as 
properties, though the property names won't show up in your calltips or 
the like).

Enjoy,
Mike

boncelet wrote:

>I want to create lots of similar classes and wonder what's the
>best/most efficient way.
>
>Pseudo-code looks like this:
>
>class Super:
>  def __init__(self,name,lots_of_other_arguments):
>    self.name=name
>    <<set rest of arguments, etc>>
>
>class A(Super):
>  def __init__(self,name='a',lots_of_other_arguments):
>    Super.__init__(self,name,lots_of_other_arguments)
>
>There will be lots of these, eg., A, B, C etc. Most of these will
>differ only in the name argument.  In a few, I might want to use only
>a subset of the "other arguments".
>
>My problem is rewriting the "lot_of_other_arguments" over and over
>again seems wasteful, error-prone, and fragile.  Is there a better
>way?
>
>Thanks,
>  Charlie Boncelet <boncelet at udel.edu>
>  
>
_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list