Mailing List Archive

Keyword calling gotcha ?
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
Keyword calling gotcha ? [ In reply to ]
"Darrell" <news@dorb.com> writes:
> Am I missing something ?

Yes.

> >>> class A:
> ... def __init__(self, **parms):
> ... pass
> ...
> >>> class B(A):
> ... def __init__(self, **parms):
> ... A.__init__(self,parms)

Try:

apply(A.__init__,(self,),parms)

Yes, it's ugly. I don't know any other way of doing it though.

HTH
Michael

> ...
> >>> 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
Keyword calling gotcha ? [ In reply to ]
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@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
>
>
Keyword calling gotcha ? [ In reply to ]
Yeah, I think the problem is that you're passing a dictionary rather
than the same set of arguments to A.__init__.

>>> def keyword_usage_example (**parms):
print parms
keyword_usage_example (parms)

... ... ... >>>
>>> keyword_usage_example (a=1,b=2)
{'b': 2, 'a': 1}
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in keyword_usage_example
TypeError: too many arguments; expected 0, got 1
>>>

There's probably a clean way to deal with this, but I don't know it.
Here is a messy way:

def alternative_keyword_usage_example (**parms):
if parms.has_key ('key_word_dictionary'):
parms = parms ['key_word_dictionary']
print parms
alternative_keyword_usage_example (key_word_dictionary = parms)

alternative_keyword_usage_example (a = 1, b = 2)

I think the guys running my newsgroup server will get pissed off if I
show you the output...

See you.
Alex.
Keyword calling gotcha ? [ In reply to ]
On 25 May 1999, Alex wrote:

> Yeah, I think the problem is that you're passing a dictionary rather
> than the same set of arguments to A.__init__.
>
> >>> def keyword_usage_example (**parms):
> print parms
> keyword_usage_example (parms)
>
> ... ... ... >>>
> >>> keyword_usage_example (a=1,b=2)
> {'b': 2, 'a': 1}
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> File "<stdin>", line 3, in keyword_usage_example
> TypeError: too many arguments; expected 0, got 1
> >>>
>
> There's probably a clean way to deal with this, but I don't know it.

Read up the documentation on the built-in apply function, which is
good for just this usage (calling another function with the same
parameters) and many other.
--
Moshe Zadka <mzadka@geocities.com>.
QOTD: My own exit is more likely to be horizontal then perpendicular.
Keyword calling gotcha ? [ In reply to ]
Replace "A.__init__(self, parms)" with "apply(A.__init__, (self,), parms)".
"A.__init__" is expecting one fixed parameter plus any number of keyword
parameters. You're calling it with two fixed parameters, one of which
happens to be a dict obtained from keyword parameters.

Darrell wrote in message ...
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
Keyword calling gotcha ? [ In reply to ]
Yes I see the problem.
But I don't like it.
Keyword parms aren't as useful as I though they might be.
Using apply all over the place is not appealing.

--Darrell

Evan Simpson <evan@tokenexchange.com> wrote in message
news:AXX23.14$KR2.13445@news.corridex.com...
> Replace "A.__init__(self, parms)" with "apply(A.__init__, (self,),
parms)".
> "A.__init__" is expecting one fixed parameter plus any number of keyword
> parameters. You're calling it with two fixed parameters, one of which
> happens to be a dict obtained from keyword parameters.
>
> Darrell wrote in message ...
> 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
>
>
>
>