[BangPypers] MetaClass in python
Sirtaj Singh Kang
sirtaj at sirtaj.net
Thu Jan 13 02:53:18 CET 2011
On 12-Jan-11, at 7:43 AM, Nitin Kumar wrote:
[snip]
>
> So {} can contain function also for the new class, if then can you
> give
> example for the same. As i need function to class being generated.
>
Yes, you can replace the {} with something like
{ 'my_method': some_function }
but the 'some_function' is very specific to your problem. Depending on
what you want in the body of the function, you have a range of
available options, from defining the function inside another function
body, like
def custom_class(original_class_name):
def replacement_method(obj):
print "custom method for", original_class_name
new_class = type(original_class_name, (object,), {'my_method':
replacement_method})
return new_class
... to using eval to create your new function from a body of generated
code in text form.
-Taj.
More information about the BangPypers
mailing list