Passing variable-length keyworded arguments list to base class constructor?
Alex Martelli
alex at magenta.com
Mon Jul 3 05:35:43 EDT 2000
More information about the Python-list mailing list
Mon Jul 3 05:35:43 EDT 2000
- Previous message (by thread): Passing variable-length keyworded arguments list to base class constructor?
- Next message (by thread): Transcendentals and Encryption (was Re: Nth digit of PI)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Jerome Quelin <jerome.quelin at insalien.org> wrote in message news:962367281.1198008296 at news.libertysurf.fr... > "Alex Martelli" <alex at magenta.com> wrote: > >> I was wondering how to pass variable-length keyworded arguments list > >> to my base-class constructor? Look at the example code below: > >Doesn't apply work for __init__ just as for any other function? > Sure it works for __init__ too, I've already tried this approch, but it doesn't > work either. In fact, I'm asking the question for __init__ because I run into > this problem when calling constructors, but the question is the same for every > inherited and overloaded method. > > When using the *args syntax, I get an error when calling my constructor because > I'm using keywords arguments. Ok, so *args is out. > When using the **args syntax, python fetches all keyworded arguments in _one_ > dictionnary in my constructor, and I want to keep the arguments in multiple > key/value pairs (and not flattened in one dictionnary) since the parent > constructor is waiting for keyworded arguments too. But that's what apply does (in part): turn a "flattened in one dictionary" set of name/value pairs into "keyworded arguments" for the function being applied. > If I use the **args syntax and then call the base constructor with: > Parent.__init__(self, args) > I got an error since it doesn't wait for a dictionnary as an argument (and args > is a dictionnary in this case) but for key=value arguments, with key and value > taken from the args dictionnary. So you should instead call apply(Parent.__init__,(self,),args), shouldn't you? Toy example: class Parent: def __init__(self, foo=23, bar=45): print foo, bar class Derived(Parent): def __init__(self, **kwargs): apply(Parent.__init__, (self,), kwargs) >>> esem.Derived(foo=99) 99 45 <esem.Derived instance at 16a21c0> >>> Of course you can alter the kwargs dictionary as required in your Derived.__init__ method for whatever special needs you may have; e.g., suppose you want to change the *default* for bar from 45 to 73 in Derived (only), you might code: class Derived(Parent): def __init__(self, **kwargs): if not kwargs.has_key('bar'): kwargs['bar']=73 apply(Parent.__init__, (self,), kwargs) and now: >>> esem.Derived(foo=99) 99 73 <esem.Derived instance at 16a6810> >>> Alex
- Previous message (by thread): Passing variable-length keyworded arguments list to base class constructor?
- Next message (by thread): Transcendentals and Encryption (was Re: Nth digit of PI)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list