根据http://blog.jobbole.com/21351/所作的代码实践。
这篇讲得不错,但以我现在的水平,用到的机会是很少的啦。。。
#coding=utf-8class ObjectCreator(object): passmy_object = ObjectCreator()# print my_objectdef echo(o): print oecho(ObjectCreator)print hasattr(ObjectCreator, 'new_attribute')ObjectCreator.new_attribute = 'foo'print hasattr(ObjectCreator, 'new_attribute')print ObjectCreator.new_attributeObjectCreatorMirror = ObjectCreatorprint ObjectCreatorMirror()def choose_class(name): if name == 'foo': class Foo(object): pass return Foo else: class Bar(object): pass return Barprint '========================'MyClass = choose_class('foo')print MyClassprint MyClass()print '============================'print type(1)print type("1")print type(ObjectCreator)print type(ObjectCreator())print '=========================='MyShinyClass = type('MyShinyClass', (), {})print MyShinyClassprint MyShinyClass()print '========================='Foo = type('Foo', (), { 'bar':True})print Fooprint Foo.barf = Foo()print fprint f.barprint '======================'FooChild = type('FooChild', (Foo,), {})print FooChildprint FooChild.barprint '==========================='def echo_bar(self): print self.barFooChild = type('FooChild', (Foo,),{ 'echo_bar': echo_bar})print hasattr(Foo, 'echo_bar')print hasattr(FooChild, 'echo_bar')my_foo = FooChild()print my_foo.echo_bar()print '============================='age = 35print age.__class__name = 'bob'print name.__class__def foo(): passprint foo.__class__class Bar(object): passb = Bar()print b.__class__print '============================'print age.__class__.__class__print name.__class__.__class__print foo.__class__.__class__print b.__class__.__class__print '============================'class UpperAttrMetaClass(type): def __new__(cls, name, bases, dct): attrs = ((name, value) for name, value in dct.items() if not name.startswith('__')) uppercase_attr = dict((name.upper(), value) for name, value in attrs) return super(UpperAttrMetaClass, cls).__new__(cls, name, bases, uppercase_attr)class Foo(object): __metaclass__ = UpperAttrMetaClass bar = 'bip'print hasattr(Foo, 'bar')print hasattr(Foo, 'BAR')f = Foo()print f.BAR print '============================'