What is wrong with this simple python metaclass? -


digression start

i learnt metaclasses in python. don't think creators of python wanted use them. mean name metaclass might not class in cases enough divert people away concept!

digression end

on question. wrote simple metaclass add default doc string classes created in module. not working:

def metest(cls,name,bases,dict):     cls.setattr(cls,'__doc__',"""default doc""")     return type(cls,(),{})  __metaclass__=metest  class test(object):     pass  print test.__doc__  t=test()  print t.__doc__ 

output:

none none 

what doing wrong?

i made example work:

def metest(name, bases, dict):     print name, bases, dict     dict['__doc__'] = """new doc"""     cls = type(name+"_meta", bases, dict)     return cls  class test(object):     "old doc"     __metaclass__ = metest  print test print test.__doc__  t = test()  print t.__doc__ 
  1. make "meta class" used.
  2. correct signature of "class creation function". cls created us.
  3. have "old" , "new" docstring in order distinguish.

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -