上次理解过一次,时间久了,就忘了。。

再学习一次。。

http://blog.jobbole.com/21351/

=======================

但是,Python中的类还远不止如此。类同样也是一种对象。是的,没错,就是对象。只要你使用关键字class,Python解释器在执行的时候就会创建一个对象。下面的代码段:

  

将在内存中创建一个对象,名字就是ObjectCreator。这个对象(类)自身拥有创建对象(类实例)的能力,而这就是为什么它是一个类的原因。但是,它的本质仍然是一个对象,于是乎你可以对它做如下的操作:

1)   你可以将它赋值给一个变量

2)   你可以拷贝它

3)   你可以为它增加属性

4)   你可以将它作为函数参数进行传递

=======================

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class ObjectCreator(object):
    pass

print ObjectCreator

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
print ObjectCreatorMirror()

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

print type(1)
")
print type(ObjectCreator)
print type(ObjectCreator())

MyShinyClass = type('MyShinyClass', (), {})
print MyShinyClass
print MyShinyClass()

Foo = type('Foo', (), {'bar': True})
print Foo
print Foo.bar
f = Foo()
print f
print f.bar

def echo_bar(self):
    print self.bar

FooChild = type('FooChild', (Foo,),{'echo_bar': echo_bar})
print FooChild
print FooChild.bar

print hasattr(Foo, 'echo_bar')
print hasattr(FooChild, 'echo_bar')

my_foo = FooChild()
my_foo.echo_bar()

age = 35
print age.__class__
print age.__class__.__class__
name = 'bob'
print name.__class__
print name.__class__.__class__
def foo(): pass
print foo.__class__
print foo.__class__.__class__
class Bar(object): pass
print Bar.__class__
print Bar.__class__.__class__
b = Bar()
print b.__class__
print b.__class__.__class__

Python无类再理解--metaclass,type的更多相关文章

  1. python元类深入理解

    1.python 中的类 在python中,类也是一个对象,只不过这个对象拥有生成实例的能力,我们一般使用class XXX来定义一个类,在python解释器执行到这个地方的时候会自动创建出这个对象, ...

  2. python 创建类先执行metaclass父类__new__ > __init__>__call__ 然后再执行自己的__new__>__init__

    class MyType(type): def __init__(self,*args,**kwargs): print("Mytype __init__",*args,**kwa ...

  3. python元类:type和metaclass

    python元类:type和metaclass python中一切皆对象,所以类本身也是对象.类有创建对象的能力,那谁来创建类的呢?答案是type. 1.用tpye函数创建一个类 class A(ob ...

  4. 洗礼灵魂,修炼python(42)--巩固篇—type内置函数与类的千丝万缕关系

    type函数的隐藏属性 相信大家都知道内置函数type是用来查看对象的数据类型的.例: 那比如我对int类查看类型呢? 有朋友会说,int是内置类啊,用自定义的应该不会这样,我们自定义一个类呢? 还是 ...

  5. python 元类理解

    原文来自:https://segmentfault.com/a/1190000011447445 学懂元类,你只需要知道两句话: 道生一,一生二,二生三,三生万物 我是谁?我从哪来里?我要到哪里去? ...

  6. 深入理解python元类

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

  7. python 元类 MetaClass

    type() 动态语言和静态语言最大的不同,就是函数和类的定义,不是编译时定义的,而是运行时动态创建的. 比方说我们要定义一个Hello的class,就写一个hello.py模块: class Hel ...

  8. 参悟python元类(又称metaclass)系列实战(一)

    写在前面 之前在看廖雪峰python系列的教程时,对元类的章节一直头大,总在思考我到底适不适合学习python,咋这么难,尤其是ORM的部分,倍受打击:后来从0到1手撸了一套ORM,才稍微进阶了一点理 ...

  9. [py]python中的特殊类class type和类的两面性图解

    生活中的模具 生活中 编程 万物都从无到有, 起于烟尘 () 生产原料,铁 object 车床-生产各类模具 元类即metaclass,对应python的class type 模具-生产各类实在的物品 ...

随机推荐

  1. linux 下恢复后台程序的方法

    一直以为这个东西不怎么重要,所以一直没怎么去记,已经第三次百度了,不想再有第四次. 如果你在终端下运行一个程序,如果这个程序正在运行,我们可以用 ctrl + z 的命令将这个程序挂到后台. desk ...

  2. Openstack Neutron 允许VM流量转发

    neutron port-update <port-id> --port-security-enabled=False --no-security-groups

  3. 【K8s】Kubernetes 最近正在看的资料

    中国移动Kubernetes多集群统一管理实践:  http://www.tuicool.com/articles/FrqQrqI#c-22517 一种新的进入容器的方式: WebSocket + D ...

  4. Spring4 学习笔记

    [9]SpEL语法 [10]Bean的生命周期:(五步)

  5. Appium 服务器参数

    # Appium 服务器参数 使用方法: `node . [标志]` ## 服务器标志所有的标志都是可选的,但是有一些标志需要组合在一起才能生效. <expand_table> |标志|默 ...

  6. ACM/ICPC 之 昂贵的聘礼-最短路解法(POJ1062)

    //转移为最短路问题,枚举必经每一个不小于酋长等级的人的最短路 //Time:16Ms Memory:208K #include<iostream> #include<cstring ...

  7. ffmpeg-20160325-snapshot-static-bin

    ffmpeg-20160325-snapshot-static.7z ./configure \ --enable-static \ --disable-shared \ --enable-gpl \ ...

  8. hdu 1972.Printer Queue 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1972 题目意思:需要模拟打印机打印.打印机里面有一些 job,每个job被赋予1-9的其中一个值,越大 ...

  9. sqlserver insert 存储过程

    -- 根据表中数据生成insert语句的存储过程Create Proc proc_insert (@tablename varchar(256))  as                        ...

  10. 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...