manually implementing staticmethod()?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 7stud

    manually implementing staticmethod()?

    Hi,

    Can someone show me how to manually implement staticmethod()? Here is
    my latest attempt:
    ----------------
    def smethod(func):

    def newFunc():
    pass

    def newGet():
    print "new get"

    newFunc.__get__ = newGet

    return newFunc

    class Test(object):

    def show(msg):
    print msg
    show = smethod(show)


    Test.show("hell o")
    --------------
    ....but I keep getting the error:

    TypeError: unbound method newFunc() must be called with Test instance
    as first argument (got str instance instead)

    I think I need to intercept the function's __get__() method in order
    to block creation of the method object, which requires that the
    unbound method call provide an instance.

  • Michael Spencer

    #2
    Re: manually implementing staticmethod()?


    "7stud" <bbxx789_05ss@y ahoo.comwrote in message
    news:1175115664 .288706.183390@ p77g2000hsh.goo glegroups.com.. .
    Hi,
    >
    Can someone show me how to manually implement staticmethod()? Here is
    my latest attempt:
    ----------------
    Raymond Hettinger can:






    Comment

    • Alex Martelli

      #3
      Re: manually implementing staticmethod()?

      7stud <bbxx789_05ss@y ahoo.comwrote:
      Hi,
      >
      Can someone show me how to manually implement staticmethod()? Here is
      Simplest way:

      class smethod(object) :
      def __init__(self, f): self.f=f
      def __call__(self, *a, **k): return self.f(*a, **k)


      Alex

      Comment

      • 7stud

        #4
        Re: manually implementing staticmethod()?

        Hi,

        Thanks for the responses.


        On Mar 28, 4:01 pm, "Michael Spencer" <m...@telcopart ners.comwrote:
        "7stud" <bbxx789_0...@y ahoo.comwrote in message
        >
        news:1175115664 .288706.183390@ p77g2000hsh.goo glegroups.com.. .Hi,
        >
        Can someone show me how to manually implement staticmethod()? Here is
        my latest attempt:
        ----------------
        >
        Raymond Hettinger can:
        >
        http://users.rcn.com/python/download...tic-methods-an...
        I was using that article to help me. My problem was I was trying to
        implement smeth() as a function rather than a class. I hacked around
        some more, and I came up with the following before peeking at this
        thread for the answer:

        class smeth(object):
        def __init__(self, func):
        self.func = func

        def __getattribute_ _(self, name):
        print "smeth -- getattribute"
        return super(smeth, self).__getattr ibute__(name)

        def __get__(self, inst, cls=None):
        print "smeth get"
        return self.func

        class Test(object):
        def __getattribute_ _(self, name):
        print "Test - gettattribute"
        return super(Test, self).__getattr ibute__(name)
        def f():
        print "executing f()"
        return 10
        f = smeth(f)

        print Test.f #displays function obj not unbound method obj!
        Test.f()

        However, my code does not seem to obey this description in the How-To
        Guide to Descriptors:

        ---------
        Alternatively, it is more common for a descriptor to be invoked
        automatically upon attribute access. For example, obj.d looks up d in
        the dictionary of obj. If d defines the method __get__, then
        d.__get__(obj) is invoked according to the precedence rules listed
        below.***The details of invocation depend on whether obj is an object
        or a class***.
        ....
        For objects, the machinery is in object.__getatt ribute__ which
        transforms b.x into type(b).__dict_ _['x'].__get__(b, type(b)).
        ....
        For classes, the machinery is in type.__getattri bute__ which
        transforms B.x into B.__dict__['x'].__get__(None, B).
        ---------

        When I examine the output from my code, Test.f does not call
        Test.__getattri bute__(), instead it calls smeth.__get__() directly.
        Yet that last sentence from the How-To Guide to Descriptors seems to
        say that Test.__getattri bute__() should be called first.


        I'm using python 2.3.5.

        On Mar 29, 9:34 am, a...@mac.com (Alex Martelli) wrote:
        Simplest way:
        >
        class smethod(object) :
        def __init__(self, f): self.f=f
        def __call__(self, *a, **k): return self.f(*a, **k)
        >
        Alex
        Interesting. That looks like a functor to me. Thanks. I notice that
        __get__() overrides __call__().



        Comment

        • Alex Martelli

          #5
          Re: manually implementing staticmethod()?

          7stud <bbxx789_05ss@y ahoo.comwrote:
          ...
          I'm using python 2.3.5.
          >
          On Mar 29, 9:34 am, a...@mac.com (Alex Martelli) wrote:
          Simplest way:

          class smethod(object) :
          def __init__(self, f): self.f=f
          def __call__(self, *a, **k): return self.f(*a, **k)

          Alex
          >
          Interesting. That looks like a functor to me. Thanks. I notice that
          __get__() overrides __call__().
          Not sure what you mean by "overrides" in this context. Accessing an
          attribute that's a descriptor in the class invokes __get__ (having a
          __get__ is the definition of "being a descriptor"); here, we have no
          conceivable use for __get__, thus we simply omit it, so that instances
          of smethod aren't descriptors and accessing them as attributes does
          nothing special whatsoever. __call__ operates when a call is made, an
          operation that is completely separate (and temporally later than) the
          "accessing as an attribute" (or whatever other "accessing" ).

          Not sure what you mean by "a functor" (a terminology that's alien to
          Python, though I'm sure other languages embrace it whole-heartedly). If
          you mean "a callable that's not a function", why, sure, an instance of
          smethod is callable, and it's not a function. Of course, there are many
          other ways of building objects that are callable and aren't functions:

          def smethod(f):
          def __new__(cls, *a, **k): return f(*a, **k)
          return type(f.__name__ ,(),dict(__new_ _=__new__))

          Is this "a functor", too? I don't really know, nor care -- it's just a
          somewhat more intricate way to make callables that aren't functions.


          Alex

          Comment

          Working...