Keyword calling gotcha ?

Robert J. Roy rjroyNoSpam at magma.ca
Tue May 25 15:06:33 EDT 1999


Hi Darrell,
    The function call expects keyword args in the standard format.
However you can use the apply function to make this work,
Try this:

>>> class A:
...     def __init__(self,**parms):
...         for parm in parms.keys():
...             print parm,'=',parms[parm]
...             setattr(self,parm,parms[parm])
... 
>>> class B(A):
... 	def __init__(self,**parms):
... 	    apply(A.__init__,(self,),parms)
... 
... 	
>>> b=B(a=1,b=2)
b = 2
a = 1

Robert Roy
RJR Informatics

On Tue, 25 May 1999 14:40:52 -0400, "Darrell" <news at dorb.com> wrote:

>Am I missing something ?
>
>>>> class A:
>...     def __init__(self, **parms):
>...             pass
>...
>>>> class B(A):
>...     def __init__(self, **parms):
>...             A.__init__(self,parms)
>...
>>>> A(a=1,b=2)
><__main__.A instance at 7f85a0>
>>>>
>>>> B(a=1,b=2)
>Traceback (innermost last):
>  File "<stdin>", line 1, in ?
>  File "<stdin>", line 3, in __init__
>TypeError: too many arguments; expected 1, got 2
>>>>
>
>--Darrell
>
>





More information about the Python-list mailing list