根据http://blog.jobbole.com/21351/所作的代码实践。

这篇讲得不错,但以我现在的水平,用到的机会是很少的啦。。。

#coding=utf-8

class ObjectCreator(object):
    pass

my_object = ObjectCreator()
# print my_object
def echo(o):
    print o

echo(ObjectCreator)
print hasattr(ObjectCreator, 'new_attribute')
ObjectCreator.new_attribute = 'foo'
print hasattr(ObjectCreator, 'new_attribute')
print ObjectCreator.new_attribute
ObjectCreatorMirror = ObjectCreator
print ObjectCreatorMirror()

def choose_class(name):
    if name == 'foo':
        class Foo(object):
            pass
        return Foo
    else:
        class Bar(object):
            pass
        return Bar

print '========================'
MyClass = choose_class('foo')
print MyClass
print MyClass()

print '============================'
print type(1)
")
print type(ObjectCreator)
print type(ObjectCreator())
print '=========================='
MyShinyClass = type('MyShinyClass', (), {})
print MyShinyClass
print MyShinyClass()
print '========================='
Foo = type('Foo', (), {'bar':True})
print Foo
print Foo.bar
f = Foo()
print f
print f.bar
print '======================'
FooChild = type('FooChild', (Foo,), {})
print FooChild
print FooChild.bar
print '==========================='
def echo_bar(self):
    print self.bar

FooChild = 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 = 35
print age.__class__
name = 'bob'
print name.__class__
def foo():  pass
print foo.__class__
class Bar(object):  pass
b = 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 '============================'

深刻理解Python中的元类(metaclass)--代码实践的更多相关文章

  1. [转]深刻理解Python中的元类(metaclass)以及元类实现单例模式

    使用元类 深刻理解Python中的元类(metaclass)以及元类实现单例模式 在看一些框架源代码的过程中碰到很多元类的实例,看起来很吃力很晦涩:在看python cookbook中关于元类创建单例 ...

  2. 深刻理解Python中的元类metaclass(转)

    本文由 伯乐在线 - bigship 翻译 英文出处:stackoverflow 译文:http://blog.jobbole.com/21351/ 译注:这是一篇在Stack overflow上很热 ...

  3. 深刻理解Python中的元类(metaclass)

    译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得 ...

  4. [转] 深刻理解Python中的元类(metaclass)

    非常详细的一篇深入讲解Python中metaclass的文章,感谢伯乐在线-bigship翻译及作者,转载收藏. 本文由 伯乐在线 - bigship 翻译.未经许可,禁止转载!英文出处:stacko ...

  5. 深刻理解Python中的元类(metaclass)【转】

    译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得 ...

  6. 深刻理解Python中的元类(metaclass)以及元类实现单例模式

    在理解元类之前,你需要先掌握Python中的类.Python中类的概念借鉴于Smalltalk,这显得有些奇特.在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段.在Python中这一点仍 ...

  7. python——深刻理解Python中的元类(metaclass)

    译注:这是一篇在Stack overflow上 很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉 ...

  8. 深刻理解Python中的元类(metaclass)(转)

    转载地址:http://blog.jobbole.com/21351/ 另外有几点理解记录下: 创建一个实例时,有时会传入参数,这些参数会同时传入 __init__() 和 __new__(),如: ...

  9. 深入理解Python中的元类(metaclass)

    原文 译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍 ...

随机推荐

  1. 1.2Hello, World!的大小

    描述 还记得在上一章里,我们曾经输出过的“Hello, World!”吗? 它虽然不是本章所涉及的基本数据类型的数据,但我们同样可以用sizeof函数获得它所占用的空间大小. 请编程求出它的大小,看看 ...

  2. [译]The multi Interface

    The multi Interfacemulti接口 The easy interface as described in detail in this document is a synchrono ...

  3. java 选择排序与冒泡排序

    选择排序与冒泡排序的特点与区别 ++++++++++++++++++++++++++++++++++++++++++++++ 选择排序 这一种简单的排序方法,它的基本思想是:R[n]第一次从R[0]~ ...

  4. SqlServer知识点-操作xml

    一.开发环境 SQL2010 二.开发过程 1.声明一个xml类型变量 DECLARE @xmlInfo XML; SET @xmlInfo = '<CompanyGroup> <C ...

  5. Redis作者:深度剖析Redis持久化

    Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能.持久存储.适应高并发应用场景等优势.它虽然起步较晚,但发展却十分迅速. 近日,Redis的作者在博客中写到, ...

  6. [转] Redis在windows下安装过程

    转载自(http://www.cnblogs.com/M-LittleBird/p/5902850.html) 一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的win ...

  7. 关于c++11中static类对象构造函数线程安全的验证

    在c++11中,static静态类对象在执行构造函数进行初始化的过程是线程安全的,有了这个特征,我们可以自己动手轻松的实现单例类,关于如何实现线程安全的单例类,请查看c++:自己动手实现线程安全的c+ ...

  8. Objective-C中copy 、retain以及ARC中新加入的strong、weak关键字的含义

    copy: 创建一个引用计数为1的对象,然后释放旧的对象 retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的引用计数为 1 Copy其实是建立了一个相同的对象,而retain不是: ...

  9. lsb_release No LSB modules are available

    lsb_release 提示: No LSB modules are available   执行: sudo apt-get install lsb-core

  10. spring boot MongoDb配置和多数据源

    配置文件: # MongoDB配置项 mongodb.base.host: 192.168.1.204 mongodb. mongodb.base.database: xxx mongodb.base ...