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

Paolino paolo_veronelli at tiscali.it
Fri Aug 12 13:09:45 CEST 2005


Dont' know where are you going with that but if what you need is 
cancelling some attributes when inheriting then probably this is a 
cleaner approach:

class Meta(type):
   def __init__(cls, name, bases, dic):
     def attributeError(*_):
       raise AttributeError
     for base in bases:
       for dont in base.privates:
         setattr(cls,dont,attributeError) #override private methods

class Foo(object):
   privates=('f',)
   def f(self):
     pass
   def g(self):
     pass

class Bar(Foo):
   __metaclass__ = Meta

b=Bar()
b.g()
b.f()

and it implies writing only one metaclass.


	

	
		
___________________________________ 
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB 
http://mail.yahoo.it



More information about the Python-list mailing list