Passing variable-length keyworded arguments list to base class constructor?
Gordon McMillan
gmcm at hypernet.com
Fri Jun 30 08:38:47 EDT 2000
Jerome Quelin wrote:
>I was wondering how to pass variable-length keyworded arguments list
>to my base-class constructor? Look at the example code below:
>
>-- File test.py
>from Tkinter import *
>class F(Frame):
> def __init__(self, **args):
> Frame.__init__(self, args)
> # Here my initialisation stuff
>F(bg='blue').mainloop()
>-- End of file
>
>When doing this, I got an error in the Tkinter module, because it does
>not expect a dictionnary for an argument, but rather a list of
>keyworded arguments.
>
>If I write:
> def __init__(self, *args):
>
>it doesn't work because I create the class instance with keyworded
>arguments:
> F(bg='blue')
>
>and the * (one star) expects just arguments. Therefore, I must use the
>** (double star) construct to fetch a dictionnary.
>But how to pass this dictionnary to the parent class constructor?
class F(Frame):
def __init__(self, *args, **kwargs):
apply(Frame.__init__, (self,)+args, kwargs)
Generally you have to muck with the kwargs dict before passing it to the
base class __init__.
- Gordon
More information about the Python-list
mailing list