Prev: Revisiting Generators and Subgenerators
Next: Don't understand behavior; instance form a class in another class'instance
From: Michel on 25 Mar 2010 18:00 Hi everyone, I'm trying to dynamically create a class. What I need is to define a class, add methods to it and later instantiate this class. Methods need to be bound to the instance though, and that's my problem. Here is what I have so far: method_template = "def test_foo(self):\ #actual test_foo\ pass" exec method_template TestClass = types.ClassType("MyTestClass", (unittest.TestCase, ), {}) TestClass.__module__ = "test" now what to do next? I looked at types.MethodType but it needs an instance to bind the method and a function object. Should I define __new__ to bind the method during instantiation? Hope this makes sense, Michel.
From: Michiel Overtoom on 25 Mar 2010 18:18 On 2010-03-25 23:00, Michel wrote: > I'm trying to dynamically create a class. What I need is to define a > class, add methods to it and later instantiate this class. Methods > need to be bound to the instance though, and that's my problem. Maybe this snippet is of any help? import functools class Template(object): pass def printmyname(self): print self.name t=Template() t.name="Pete" t.printmyname=functools.partial(printmyname,t) u=Template() u.name="Mary" u.printmyname=functools.partial(printmyname,u) t.printmyname() u.printmyname() Greetings, -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html
From: Patrick Maupin on 25 Mar 2010 18:34 On Mar 25, 5:00 pm, Michel <michel.metz...(a)gmail.com> wrote: > Hi everyone, > > I'm trying to dynamically create a class. What I need is to define a > class, add methods to it and later instantiate this class. Methods > need to be bound to the instance though, and that's my problem. Here > is what I have so far: Well, you should just fill your empty dict with function definitions, BEFORE you build the class. That's easiest. Also, you can just use type: def foo(*whatever): print foo bar = type('MyDynamicClass', (object,), dict(foo=foo)) HTH, Pat
From: I V on 25 Mar 2010 21:53 On Thu, 25 Mar 2010 15:00:35 -0700, Michel wrote: > I'm trying to dynamically create a class. What I need is to define a > class, add methods to it and later instantiate this class. Methods need > to be bound to the instance though, and that's my problem. Here is what > I have so far: I'm not entirely sure what you mean by binding methods to the instance. Do you mean you need to dynamically add methods to a specific instance? Or that you need to add methods to a class, such that they can be invoked on specific instances? For the latter, just do: TestClass.test_foo = test_foo For the former, try: tc = TestClass() tc.test_foo = types.MethodType(test_foo, tc)
From: Michel on 26 Mar 2010 11:00
Well, I don't have the reference to the instance. The class is actually instantiated later by a the unittest library. On Mar 25, 6:18 pm, Michiel Overtoom <mot...(a)xs4all.nl> wrote: > On 2010-03-25 23:00, Michel wrote: > > > I'm trying to dynamically create a class. What I need is to define a > > class, add methods to it and later instantiate this class. Methods > > need to be bound to the instance though, and that's my problem. > > Maybe this snippet is of any help? > > import functools > > class Template(object): > pass > > def printmyname(self): > print self.name > > t=Template() > t.name="Pete" > t.printmyname=functools.partial(printmyname,t) > > u=Template() > u.name="Mary" > u.printmyname=functools.partial(printmyname,u) > > t.printmyname() > u.printmyname() > > Greetings, > > -- > "The ability of the OSS process to collect and harness > the collective IQ of thousands of individuals across > the Internet is simply amazing." - Vinod Valloppillilhttp://www.catb.org/~esr/halloween/halloween4.html |