Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63642
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.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.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'argument': 0.05; 'assignment': 0.07; 'patch.': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'suggest': 0.14; 'callable': 0.16; 'lambda:': 0.16; 'namespace.': 0.16; 'roy': 0.16; 'typeerror:': 0.16; 'unbound': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'seems': 0.21; '(in': 0.22; 'print': 0.22; 'error': 0.23; "i've": 0.25; 'define': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'patch': 0.29; 'skip:@ 10': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'file': 0.32; 'class': 0.32; 'skip:- 30': 0.32; 'another': 0.32; 'running': 0.33; '(most': 0.33; 'could': 0.34; 'no,': 0.35; 'received:google.com': 0.35; 'doing': 0.36; "didn't": 0.36; 'method': 0.36; 'subject:?': 0.36; 'error.': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'even': 0.60; 'first': 0.61; 'kind': 0.63; 'smith': 0.68; 'subject:skip:M 10': 0.84 |
| 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=0Pbi79OZSHe9caNb3wu8wkG4/Kfwa9nNpysyBFQcc2E=; b=gEYJKyHzJzL1kBx6y9usjj3MBvll5Cfnuz13VZ5LG12khfIkvjYWQaAR5HNJUQ7aZ5 Hw5hhev407MoOFfvP2ZYwT0lNxO3ljBfKlmKke2A6KWU1XDsJnPI+5moH16ThQmadgLG svw83o/c/KSj6r5pyRhSmBGcjZy2lJv6M+BlNIOWNvCjQAJW/c9A/1Du00ufKzPKKLE7 /3bUww/ni73ubpkVmXn9V2KUFobZbWUFtgWCbJKgDcw0+tSZuEWvzr5f+6a1AuCH+Or2 bQmCrqI5/niu4Rl93Ivs052gykKUQfQlj22TC1oUYb54Zm1q86iCYfyACaHoibno48rs uobA== |
| X-Received | by 10.68.143.100 with SMTP id sd4mr9472011pbb.0.1389339150435; Thu, 09 Jan 2014 23:32:30 -0800 (PST) |
| MIME-Version | 1.0 |
| In-Reply-To | <roy-CE7D46.00231110012014@news.panix.com> |
| References | <roy-CE7D46.00231110012014@news.panix.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Fri, 10 Jan 2014 00:31:50 -0700 |
| Subject | Re: Monkeypatching a staticmethod? |
| To | Python <python-list@python.org> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| 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.5292.1389339158.18130.python-list@python.org> (permalink) |
| Lines | 53 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1389339158 news.xs4all.nl 2927 [2001:888:2000:d::a6]:55353 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:63642 |
Show key headers only | View raw
On Thu, Jan 9, 2014 at 10:23 PM, Roy Smith <roy@panix.com> wrote: > This is kind of surprising. I'm running Python 2.7.1. I've got a class > with a staticmethod that I want to monkeypatch with a lambda: > > ---------------------------------- > class Foo: > @staticmethod > def x(): > return 1 > > Foo.x = lambda: 2 > print Foo.x() > ---------------------------------- > > What's weird is that it seems to remember that x is a staticmethod > despite having been patched: > > Traceback (most recent call last): > File "static.py", line 8, in <module> > Foo.x() > TypeError: unbound method <lambda>() must be called with Foo instance as > first argument (got nothing instead) No, if it were still a staticmethod then the call would work without error. That error is the same that you would get in Python 2 if you defined the function directly in the class without the staticmethod decorator. (In Python 3 the call succeeds as long as it's made from the class and not from an instance). > What seems to work is to patch it with another staticmethod: > > ---------------------------------- > class Foo: > @staticmethod > def x(): > return 1 > > @staticmethod > def x(): > return 2 > > Foo.x = x > print Foo.x() > ---------------------------------- > > $ python static.py > 2 > > I didn't even know you could define a staticmethod outside of a class! I suggest defining x as a normal function and writing the assignment as "Foo.x = staticmethod(x)" to keep x callable from the global namespace. Or just del it after doing the monkey patch.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Monkeypatching a staticmethod? Roy Smith <roy@panix.com> - 2014-01-10 00:23 -0500
Re: Monkeypatching a staticmethod? Ian Kelly <ian.g.kelly@gmail.com> - 2014-01-10 00:31 -0700
Re: Monkeypatching a staticmethod? Piet van Oostrum <piet@vanoostrum.org> - 2014-01-10 15:04 +0100
csiph-web