no unbound methods in py3k

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Heller

    no unbound methods in py3k

    I'm currently using code like this to create unbound methods
    from functions and stick them into classes:

    method = new.instancemet hod(raw_func, None, cls)
    setattr(cls, name, method)

    Ok, python 2.6, run with the -3 flag, gives a warning that the new
    module is going away in python 3.0, so the equivalent code is:

    method = types.MethodTyp e(raw_func, None, cls)
    setattr(cls, name, method)

    However, this code will not work in Python 3.0 because there are
    no unbound methods any longer. The only way that I found so far
    is this code:

    method = lambda self, *args: raw_func(self, *args)
    setattr(cls, name, method)

    or the equivalent:

    def method(self, *args):
    return raw_func(self, *args)
    setattr(cls, name, method)

    but this is very ugly, imo. Is there another way?
    The raw_func instances that I have are not descriptors (they
    do not implement a __get__() method...)

    Thanks,
    Thomas
  • Christian Heimes

    #2
    Re: no unbound methods in py3k

    Thomas Heller wrote:
    but this is very ugly, imo. Is there another way?
    The raw_func instances that I have are not descriptors (they
    do not implement a __get__() method...)
    I've written PyInstanceMetho d_Type for this use case. It's not (yet)
    available for Python code. Barry hasn't decided whether he should expose
    the type so late in the release cycle or not. See
    http://bugs.python.org/issue3787 and


    Christian

    Comment

    • Thomas Heller

      #3
      Re: no unbound methods in py3k

      Christian Heimes schrieb:
      Thomas Heller wrote:
      >but this is very ugly, imo. Is there another way?
      >The raw_func instances that I have are not descriptors (they
      >do not implement a __get__() method...)
      >
      I've written PyInstanceMetho d_Type for this use case. It's not (yet)
      available for Python code. Barry hasn't decided whether he should expose
      the type so late in the release cycle or not. See
      http://bugs.python.org/issue3787 and

      >
      Ok, so one has to write an extension to access or expose it.

      Oh, wait - there's ctypes:

      Python 3.0rc1 (r30rc1:66507, Sep 18 2008, 14:47:08) [MSC v.1500 32 bit (Intel)] on win32
      Type "help", "copyright" , "credits" or "license" for more information.
      >>from ctypes import *
      >>pythonapi.PyI nstanceMethod_N ew.restype = py_object
      >>pythonapi.PyI nstanceMethod_N ew.argtypes = [py_object]
      >>instancemetho d = pythonapi.PyIns tanceMethod_New
      >>>
      >>class Example:
      .... pass
      ....
      >>Example.id = instancemethod( id)
      >>>
      >>x = Example()
      >>x.id()
      12597296
      >>id(x)
      12597296
      >>>
      Thomas

      Comment

      • Terry Reedy

        #4
        Re: no unbound methods in py3k

        Thomas Heller wrote:
        Christian Heimes schrieb:
        >Thomas Heller wrote:
        >>but this is very ugly, imo. Is there another way?
        >>The raw_func instances that I have are not descriptors (they
        >>do not implement a __get__() method...)
        >I've written PyInstanceMetho d_Type for this use case. It's not (yet)
        >available for Python code. Barry hasn't decided whether he should expose
        >the type so late in the release cycle or not. See
        >http://bugs.python.org/issue3787 and
        >http://docs.python.org/dev/3.0/c-api...nceMethod_Type
        >>
        >
        Ok, so one has to write an extension to access or expose it.
        >
        Oh, wait - there's ctypes:
        >
        Python 3.0rc1 (r30rc1:66507, Sep 18 2008, 14:47:08) [MSC v.1500 32 bit (Intel)] on win32
        Type "help", "copyright" , "credits" or "license" for more information.
        >>>from ctypes import *
        >>>pythonapi.Py InstanceMethod_ New.restype = py_object
        >>>pythonapi.Py InstanceMethod_ New.argtypes = [py_object]
        >>>instancemeth od = pythonapi.PyIns tanceMethod_New
        >>>>
        >>>class Example:
        ... pass
        ...
        >>>Example.id = instancemethod( id)
        >>>>
        >>>x = Example()
        >>>x.id()
        12597296
        >>>id(x)
        12597296
        A pyCapi module that exposed via ctypes useful C functions not otherwise
        accessible, with predefinition of restype and argtypes and anything else
        needed, might make a nice addition to PyPI if not the stdlib. You have
        done two here and I believe others have posted others.

        tjr

        Comment

        • Thomas Heller

          #5
          Re: no unbound methods in py3k

          Terry Reedy schrieb:
          Thomas Heller wrote:
          >Christian Heimes schrieb:
          >>I've written PyInstanceMetho d_Type for this use case. It's not (yet)
          >>available for Python code.>Oh, wait - there's ctypes:
          >>
          >Python 3.0rc1 (r30rc1:66507, Sep 18 2008, 14:47:08) [MSC v.1500 32 bit (Intel)] on win32
          >Type "help", "copyright" , "credits" or "license" for more information.
          >>>>from ctypes import *
          >>>>pythonapi.P yInstanceMethod _New.restype = py_object
          >>>>pythonapi.P yInstanceMethod _New.argtypes = [py_object]
          >>>>instancemet hod = pythonapi.PyIns tanceMethod_New
          >>>>>
          >>>>class Example:
          >... pass
          >...
          >>>>Example.i d = instancemethod( id)
          >>>>>
          >>>>x = Example()
          >>>>x.id()
          >12597296
          >>>>id(x)
          >12597296
          >
          A pyCapi module that exposed via ctypes useful C functions not otherwise
          accessible, with predefinition of restype and argtypes and anything else
          needed, might make a nice addition to PyPI if not the stdlib. You have
          done two here and I believe others have posted others.
          Well, Lenard Lindstrom has some time ago contributed a module like that
          which is available in the (more or less unmaintained) ctypeslib project:


          However, it was probably more meant to provide a complete Python C api,
          instead of concentrating on stuff not available to Python otherwise.

          Thomas

          Comment

          • Christian Heimes

            #6
            Re: no unbound methods in py3k

            Thomas Heller wrote:
            Ok, so one has to write an extension to access or expose it.
            >
            Oh, wait - there's ctypes:
            I wrote the type to help the Pyrex and Cython developers to port their
            software to 3.0. I planed to expose the type as
            __builtin__.ins tancemethod but forgot it. Maybe we can convince Barry
            together. He still considers my bug as a release blocker for 3.0.

            Christian

            Comment

            • Thomas Heller

              #7
              Re: no unbound methods in py3k

              Christian Heimes schrieb:
              Thomas Heller wrote:
              >Ok, so one has to write an extension to access or expose it.
              >>
              >Oh, wait - there's ctypes:
              >
              I wrote the type to help the Pyrex and Cython developers to port their
              software to 3.0. I planed to expose the type as
              __builtin__.ins tancemethod but forgot it. Maybe we can convince Barry
              together. He still considers my bug as a release blocker for 3.0.
              >
              Issue 3787 is marked as release blocker, but for 3.1 IIUC. Would it help
              if I post my use case to the tracker?

              Thomas

              Comment

              Working...