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


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

Monkeypatching a staticmethod?

Started byRoy Smith <roy@panix.com>
First post2014-01-10 00:23 -0500
Last post2014-01-10 15:04 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#63639 — Monkeypatching a staticmethod?

FromRoy Smith <roy@panix.com>
Date2014-01-10 00:23 -0500
SubjectMonkeypatching a staticmethod?
Message-ID<roy-CE7D46.00231110012014@news.panix.com>
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)

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 suspect this post is really just my way of admitting that while I've 
used staticmethods plenty, I've never fully understood the details of 
what happens when you construct them :-)

[toc] | [next] | [standalone]


#63642

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-01-10 00:31 -0700
Message-ID<mailman.5292.1389339158.18130.python-list@python.org>
In reply to#63639
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.

[toc] | [prev] | [next] | [standalone]


#63647

FromPiet van Oostrum <piet@vanoostrum.org>
Date2014-01-10 15:04 +0100
Message-ID<m28uuoue48.fsf@cochabamba.vanoostrum.org>
In reply to#63642
Ian Kelly <ian.g.kelly@gmail.com> writes:

> 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.

You can use Foo.x = staticmethod(lambda: 2)
-- 
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

[toc] | [prev] | [standalone]


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


csiph-web