Re: Adding bound methods dynamically...



Kevin Little wrote:

I want to dynamically add or replace bound methods in a class. I want


I asked a seemingly-unrelated question a week or so ago, and learned something interesting:

Python 2.3.4 (#2, Jul 12 2004, 12:46:36)
[GCC 3.3] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo(self):
...     print "foo called"
...
>>> class C(object):
...     pass
...
>>> type(foo)
<type 'function'>
>>> C.foo = foo
>>> type(C.foo)
<type 'instancemethod'>
>>> c = C()
>>> c.foo()
foo called
>>> type(c.foo)
<type 'instancemethod'>
>>>

I.e. assigning a normal function object to a class object turns it into a member function!

You can read more in the thread with the subject 'keeping a ref to a non-member function in a class'.
.




Relevant Pages

  • [ANN] main-3.0.1
    ... def runputs 'installing...' ... def runputs 'uninstalling...' ... and auto-generation of usage messages can really streamline ... int(foo): the cast is int, ...
    (comp.lang.ruby)
  • virtual metaclasses explanation
    ... def ClassA ... class ClassB < ClassC ... stuart = ClassB.methodY ... def m1"foo" end ...
    (comp.lang.ruby)
  • [ANN] main-2.8.3
    ... a class factory and dsl for generating command line programs real ... def runputs 'installing...' ... and auto-generation of usage messages can really ... int(foo): the cast is int, ...
    (comp.lang.ruby)
  • Re: Getting rid of "self."
    ... def __init__: ... > basis of information available at the time the decorator executes. ... I am content with declaring them like the selfless ... foo = _foo ...
    (comp.lang.python)
  • Way to intercept method calls? - Reopened
    ... def method_missing ... The problem with this pattern is that it is imperative. ... library defines a method on Object after Foo is loaded, ... slate for Foo breaks. ...
    (comp.lang.ruby)