Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79041
| From | Dave Angel <davea@davea.name> |
|---|---|
| Subject | Re: TypeError: 'kwarg' is an invalid keyword argument for this function |
| Date | 2014-10-13 06:04 -0400 |
| Organization | news.gmane.org |
| References | <CAOoj+KAnZay7P8TPB06gn+f1ckAYbBp8yvr8TFxbD_5wQD94hA@mail.gmail.com> <CALwzidmbcXDVt1ObDEaMG31ueEAE-tc2mvRjnzCpEhN2sE3=BA@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14817.1413194487.18130.python-list@python.org> (permalink) |
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.
--
DaveA
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: TypeError: 'kwarg' is an invalid keyword argument for this function Dave Angel <davea@davea.name> - 2014-10-13 06:04 -0400 Re: TypeError: 'kwarg' is an invalid keyword argument for this function alex23 <wuwei23@gmail.com> - 2014-10-14 15:55 +1000
csiph-web