__init__ keyword param for sub-class?

Fred L. Drake, Jr. fdrake at acm.org
Thu Dec 23 14:17:59 EST 1999


Grant Edwards writes:
 > I want to sub-class something that takes one positional and a
 > whole slew of keyword arguements when the object is
 > instantiated, and I want to add one keyword argument (keyword
 > 'myoption' in the below example) to be handled by my method,
 > and pass the rest on the the super-class.  I couldn't find any

Grant,
  That's what I've always done.  There is at least one proposal to
extend the call syntax to make the apply() unnecessary, but it's not
in the core (though I seem to recall a patch to actually implement
it).
  I think your example would become:

------------------------------------------------------------------------
class execWindow(Pmw.ScrolledText):
    
    def __init__(self, *posArgs, **keyArgs):
        
        if keyArgs.has_key('myoption'):
            self.__myoption = keyArgs['myoption']
            del keyArgs['myoption']
            
        return Pmw.ScrolledText.__init__(self, *posArgs, **keyArgs)
------------------------------------------------------------------------

  I think it would be nice to have a way to specify that an argument
*must* be given as a keyword parameter, and have it never be filled in 
from positional parameters.  Perhaps something lispish:

    def __init__(self, *posArgs, **keyArgs, :myoption):
        self.__myoption = myoption
        return Pmw.ScrolledText.__init__(self, *posArgs, **keyArgs)

  I don't expect anyone would actually want to overload ':' like that, 
and I don't really care what character is used, but this functionality 
would be *very* nice to have; I've had to do the dictionary whacking
just often enough that I think it's really a fragile way to do it.


  -Fred

--
Fred L. Drake, Jr.	  <fdrake at acm.org>
Corporation for National Research Initiatives




More information about the Python-list mailing list