[BangPypers] MetaClass in python
steve at lonetwin.net
Tue Jan 11 14:39:02 CET 2011
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
> 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
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()
>>> def replacement(the_object):
... print "Replaced"
>>> UpdateMe.replace_me = replacement
(hehehe, voice in my head just said Python is dyna-effing-mic, bi***es !!)
random spiel: http://lonetwin.net/
what i'm stumbling into: http://lonetwin.stumbleupon.com/
More information about the BangPypers