George Flaherty wrote:
Hello,

I am trying to port over some old code from Ruby into Python.  In my old ruby 
code I had a UnitTest class that created a bunch of test methods (i.e. def 
test_MyTestFunction) dynamically through the ruby method 
define_method(http://www.ruby-doc.org/core/classes/Module.html#M000396).

This functionally allowed me to create any number of methods dynamically within 
a particular class. My problem is I have never done this nor can find any 
examples of this within python and I am pretty sure python can handle this?

If anyone could point me in the right direction?
See the new module's instancemethod method.

>>> import new
>>> class A:
...     pass
...
>>> a=A()
>>> a.f = new.instancemethod(lambda self:'foo', a, A)
>>> a.f()
'foo'


--
Bob Gailer
919-636-4239 Chapel Hill, NC

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to