python整理十四——元类

元类,一个新的概念

 

  1. #coding=utf-8
  2. class BCls(type):
  3.     def __init__(cls, name, bases, attr_dic):
  4.         super(BCls, cls).__init__(name, bases, attr_dic)
  5.         
  6.         if '__str__' not in attr_dic:
  7.             raise TypeError('overriding the mothod of __str__')
  8.         
  9. class Foo(object):
  10.     __metaclass__ = BCls
  11.     
  12.     def out(self, v):
  13.         print '-'*10
  14.         print v
  15.     def __str__(self):
  16.         return 'class.Foo'
  17.         
  18. if __name__ == '__main__':
  19.     Foo().out('test')   
  20.     print 'end___'             

你可能感兴趣的:(python整理十四——元类)