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__
- make "meta class" used.
- correct signature of "class creation function".
cls
created us. - have "old" , "new" docstring in order distinguish.
Comments
Post a Comment