Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79042 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2014-10-13 13:33 +0200 |
| Last post | 2014-10-13 13:33 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: TypeError: 'kwarg' is an invalid keyword argument for this function Peter Otten <__peter__@web.de> - 2014-10-13 13:33 +0200
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-10-13 13:33 +0200 |
| Subject | Re: TypeError: 'kwarg' is an invalid keyword argument for this function |
| Message-ID | <mailman.14818.1413199996.18130.python-list@python.org> |
Dave Angel wrote:
> Ian Kelly <ian.g.kelly@gmail.com> Wrote in
> message:
>> On Sun, Oct 12, 2014 at 6:55 AM, roro codeath
>> <rorocodeath@gmail.com> wrote:
>>> How to implement it in my class?
>>>
>>> class Str(str):
>>> def __init__(self, *args, **kwargs):
>>> pass
>>>
>>> Str('smth', kwarg='a')
>>
>> The error is coming from the __new__ method. Because str is an
>> immutable type, you should override the __new__ method, not __init__.
>> Example:
>>
>> class Str(str):
>> def __new__(cls, *args, **kwargs):
>> return super().__new__(cls, args[0])
>>
>>>>> Str('smth', kwarg='a')
>> 'smth'
>>
>
> It would also help to spell it the same. In the OP's
> implementation, he defined kwargs, and tried to use it as
> kwarg.
That is consistent as should become clear when using something completely
different:
>>> class Str(str):
... def __init__(self, *args, **kwargs): pass
...
>>> Str("smth", alpha="beta")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'alpha' is an invalid keyword argument for this function
Back to top | Article view | comp.lang.python
csiph-web