Turning builtin functions into methods.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jacek Generowicz

    #1

    Turning builtin functions into methods.

    Functions defined in Python have type types.FunctionT ype, and are
    descriptors whose __get__ method turns them into bound or unbound
    methods. Functions defined in extension modules have type
    types.BuiltinFu nctionType, and have no __get__ method. Adding them as
    attributes to classes and calling them through an instance of the
    class does not result in them being called as methods: self is lost.

    What's the simplest way of getting around this ?

    Some background: I'm trying to speed up my application by recoding, in
    C, the Python functions which are being called in my inner loops. I
    have a number of classes which have a single method being called in
    the inner loops, so, rather than recoding the whole class, I'd like to
    recode only the relevant method, and glue it onto the class, like this

    class foo:
    def this(self, ...):
    ...
    def that(self, ...):
    ...

    import speedup

    foo.the_other = speedup.the_oth er

    where the_other is implemented in C, but is equivalent to

    def the_other(self, ...):
    ...


    Any suggestions ?

    Thanks,



    PS. I'm a bit disappointed that Python makes this distinction between
    functions defined in extension modules, and ones defined in pure
    Python, but I guess that there are good practical reasons for it.
  • Jacek Generowicz

    #2
    Re: Turning builtin functions into methods.

    Jacek Generowicz <jacek.generowi cz@cern.ch> writes:
    [color=blue]
    > Functions defined in Python have type types.FunctionT ype, and are
    > descriptors whose __get__ method turns them into bound or unbound
    > methods. Functions defined in extension modules have type
    > types.BuiltinFu nctionType, and have no __get__ method. Adding them as
    > attributes to classes and calling them through an instance of the
    > class does not result in them being called as methods: self is lost.
    >
    > What's the simplest way of getting around this ?[/color]

    By trial and error, I seem to have found that passing None as the
    second argument (the instance) to new.instancemet hod, does the trick.

    The library reference manual confirms that it is actually supposed to
    work this way. It's a pity that the builtin documentation does not:

    Help on built-in function instancemethod:

    instancemethod( ...)
    Create a instance method object from (FUNCTION, INSTANCE, CLASS).
    (END)

    Comment

    • Michael Hudson

      #3
      Re: Turning builtin functions into methods.

      Jacek Generowicz <jacek.generowi cz@cern.ch> writes:
      [color=blue]
      > Jacek Generowicz <jacek.generowi cz@cern.ch> writes:
      >[color=green]
      > > Functions defined in Python have type types.FunctionT ype, and are
      > > descriptors whose __get__ method turns them into bound or unbound
      > > methods. Functions defined in extension modules have type
      > > types.BuiltinFu nctionType, and have no __get__ method. Adding them as
      > > attributes to classes and calling them through an instance of the
      > > class does not result in them being called as methods: self is lost.
      > >
      > > What's the simplest way of getting around this ?[/color]
      >
      > By trial and error, I seem to have found that passing None as the
      > second argument (the instance) to new.instancemet hod, does the trick.[/color]

      PyDescr_NewMeth od from C maybe?
      [color=blue]
      > The library reference manual confirms that it is actually supposed to
      > work this way. It's a pity that the builtin documentation does not:[/color]

      The patch manager is over there --->

      Cheers,
      mwh

      --[color=blue]
      > With Python you can start a thread, but you can't stop it. Sorry.
      > You'll have to wait until reaches the end of execution.[/color]
      So, just the same as c.l.py, then?
      -- Cliff Wells & Steve Holden, comp.lang.pytho n

      Comment

      Working...