[BangPypers] MetaClass in python
steve
steve at lonetwin.net
Tue Jan 11 14:39:02 CET 2011
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/
More information about the BangPypers
mailing list