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


Groups > comp.lang.python > #79016

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'method.': 0.07; "subject:' ": 0.07; '*args,': 0.09; 'immutable': 0.09; 'method,': 0.09; 'override': 0.09; 'type,': 0.09; 'def': 0.12; '**kwargs):': 0.16; '__new__': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'error': 0.23; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'class': 0.32; 'skip:_ 10': 0.34; 'received:google.com': 0.35; 'should': 0.36; 'implement': 0.38; 'to:addr:python-list': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'subject:this': 0.83
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=8wEnJVKewuGdGN7iRh53Z3okbpzvGsx/oUnwfrYA9/Q=; b=KVyi7VwT309NR3EnFPx03RjImXSImnI+ESp+Ka8fJSO87SKlo8463WhZuF127dEGpW ZTo3DVc5YqxH2KKFYeh9KdfZN8CkoNWGLGMc00UuUOjB0tftFaMfq5vIpMyELGlQclTC wWXvH4I/8U30iRHmARLlX3r/5TdMcJG+Q87axn9JxyfYJ0Q4cCGwN71QlG/c4CARf0O6 NULzk2FiBMX01caR0dGZwEXj1tZgIjSL8pw8XJfs4fprPuEVfbOaJJYjSkfkaWXnb+c2 uDaB+N21GHGat9DjaUGEykPV1j1hfFXPApDCvPKLCks2IOb0wFhXT/kIxMpPI8lpmi7a jfbQ==
X-Received by 10.68.255.229 with SMTP id at5mr19686441pbd.16.1413139554341; Sun, 12 Oct 2014 11:45:54 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <CAOoj+KAnZay7P8TPB06gn+f1ckAYbBp8yvr8TFxbD_5wQD94hA@mail.gmail.com>
References <CAOoj+KAnZay7P8TPB06gn+f1ckAYbBp8yvr8TFxbD_5wQD94hA@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 12 Oct 2014 12:45:14 -0600
Subject Re: TypeError: 'kwarg' is an invalid keyword argument for this function
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14800.1413139563.18130.python-list@python.org> (permalink)
Lines 19
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1413139563 news.xs4all.nl 2902 [2001:888:2000:d::a6]:35183
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79016

Show key headers only | View raw


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'

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: TypeError: 'kwarg' is an invalid keyword argument for this function Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-12 12:45 -0600

csiph-web