Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79026 > unrolled thread

Re: TypeError: 'kwarg' is an invalid keyword argument for this function

Started byTerry Reedy <tjreedy@udel.edu>
First post2014-10-12 20:49 -0400
Last post2014-10-12 20:49 -0400
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.


Contents

  Re: TypeError: 'kwarg' is an invalid keyword argument for this function Terry Reedy <tjreedy@udel.edu> - 2014-10-12 20:49 -0400

#79026 — Re: TypeError: 'kwarg' is an invalid keyword argument for this function

FromTerry Reedy <tjreedy@udel.edu>
Date2014-10-12 20:49 -0400
SubjectRe: TypeError: 'kwarg' is an invalid keyword argument for this function
Message-ID<mailman.14807.1413161371.18130.python-list@python.org>
On 10/12/2014 2:45 PM, Ian Kelly wrote:
> 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__.

or, if one is mere adding a method, do not add __new__ either.

> Example:
>
> class Str(str):
>      def __new__(cls, *args, **kwargs):
>          return super().__new__(cls, args[0])
>
>>>> Str('smth', kwarg='a')
> 'smth'
>


-- 
Terry Jan Reedy

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web