[BangPypers] MetaClass in python

Nitin Kumar nitin.nitp at gmail.com
Wed Jan 12 05:47:03 CET 2011

Hi Steve,

The Purpose is:
there is one module unittest2 which can do testing for our test scripts and
generate report.
Some of its unittest2 internal function churns out different test cases from
module as per given parameter (i am using discover function).

Now there are some test case which need to be skipped according to some
So according to the prerequisite we need to replace existing test case class
and its function with new one (with same class name and function name but
different steps inside the function, which will help in skipping the test
case (by using unittest2.SkipTest())

So i do know who to create a class and its function dynamically at

What I am looking is, I have class name and function names during runtime
and i can create the class using these variables.

Nitin K

On Tue, Jan 11, 2011 at 7:09 PM, steve <steve at lonetwin.net> wrote:

> Hi Nitin,
> On 01/11/2011 01:15 PM, Nitin Kumar wrote:
>> Hi all,
>> I am trying to create one meta Class which can create classes at runtime.
>> This I am able to achieve by the code snippet below
>> [...snip...]
>> but I am looking for one function for this class created above, which will
>> do some activities (say print some data) at run time.
>> what all changes can be added to MetaClass to achieve this??
>> or is there any better way for the same.
>> The above things I am doing to replace one existing class and its function
>> attribute depending on some condition. So I want to create new class
>> itself
>> using MetaClass (bcoz there can be various class which need to be created
>> at
>> runtime)
> Taking one step back can you describe what exactly are you trying to do and
> why do you think Metaclasses are the way to do it ? Maybe there is an
> existing Design Pattern to solve your problem. If you simply wanted to
> change a method for a class you can do it by:
> [steve at laptop ~]$ python
> Python 2.7 (r27:82500, Sep 16 2010, 18:02:00)
> [GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> class UpdateMe:
> ...     def replace_me(self):
> ...             print "Not replaced"
> ...
> >>> a = UpdateMe()
> >>> a.replace_me()
> Not replaced
> >>> def replacement(the_object):
> ...     print "Replaced"
> ...
> >>> UpdateMe.replace_me = replacement
> >>> a.replace_me()
> Replaced
> >>>
> (hehehe, voice in my head just said Python is dyna-effing-mic, bi***es !!)
> cheers,
> - steve
> --
> random spiel: http://lonetwin.net/
> what i'm stumbling into: http://lonetwin.stumbleupon.com/
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers

Nitin K

More information about the BangPypers mailing list