How to Adding Functionality to a Class by metaclass(not by inherit)

Steven Bethard steven.bethard at gmail.com
Fri Aug 12 00:55:12 EDT 2005


kyo guan wrote:
> How to Adding Functionality to a Class by metaclass(not by inherit)
> 
[snip]
> 		
> class MetaFoo(type):
> 	def __init__(cls, name, bases, dic):
> 		super(MetaFoo, cls).__init__(name, bases, dic)
> 		
> 		for n, f in inspect.getmembers(Foo, inspect.ismethod):
>                 	setattr(cls, n, f)
                                         ^^^^^^^^^
                                         f.im_func

See if that works.  I think it should.

But the real question is why you want to do this.  Why can't you just 
inherit from Foo?

STeVe



More information about the Python-list mailing list