Code practices based on http://blog.jobbole.com/21351/.
This is a good speech, but at my present level, the opportunity to use is very few ...
#Coding=utf-8classObjectcreator (object):PassMy_object=Objectcreator ()#Print My_objectdefEcho (o):PrintOecho (objectcreator)PrintHasattr (Objectcreator,'New_attribute') Objectcreator.new_attribute='Foo'PrintHasattr (Objectcreator,'New_attribute')PrintObjectcreator.new_attributeobjectcreatormirror=ObjectcreatorPrintObjectcreatormirror ()defChoose_class (name):ifName = ='Foo': classFoo (object):Pass returnFooElse: classBar (object):Pass returnBarPrint '========================'MyClass= Choose_class ('Foo')PrintMyClassPrintMyClass ()Print '============================'PrintType (1)PrintType"1")Printtype (objectcreator)Printtype (Objectcreator ())Print '=========================='Myshinyclass= Type ('Myshinyclass', (), {})PrintMyshinyclassPrintMyshinyclass ()Print '========================='Foo= Type ('Foo', (), {'Bar': True})PrintFooPrintFoo.barf=Foo ()PrintFPrintF.barPrint '======================'Foochild= Type ('Foochild', (Foo,), {})PrintFoochildPrintFoochild.barPrint '==========================='defEcho_bar (self):PrintSelf.barfoochild= Type ('Foochild', (Foo,), {'Echo_bar': Echo_bar})PrintHasattr (Foo,'Echo_bar')PrintHasattr (Foochild,'Echo_bar') My_foo=Foochild ()PrintMy_foo.echo_bar ()Print '=============================' Age= 35PrintAge.__class__name='Bob'PrintName.__class__defFoo ():PassPrintFoo.__class__classBar (object):Passb=Bar ()PrintB.__class__Print '============================'PrintAge.__class__.__class__PrintName.__class__.__class__PrintFoo.__class__.__class__PrintB.__class__.__class__Print '============================'classUpperattrmetaclass (type):def __new__(CLS, name, bases, DCT): Attrs= ((name, value) forName, valueinchDct.items ()if notName.startswith ('__')) Uppercase_attr= Dict ((Name.upper (), value) forName, valueinchattrs)returnSuper (Upperattrmetaclass, CLS).__new__(CLS, name, bases, uppercase_attr)classFoo (object):__metaclass__=Upperattrmetaclass Bar='Bip'PrintHasattr (Foo,'Bar')PrintHasattr (Foo,'BAR') F=Foo ()PrintF.barPrint '============================'
Deep understanding of the meta-Class (Metaclass) in Python-code practices