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

再学习一次。。

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. qt-5.6.0 移植之纯净的linux文件系统的建立

    为什么要建立一个最纯净的文件系统,一开始是想在qt-4.8.5的文件系统基础之上加东西,慎重想了一下,这方法行不通,以为有很多东西不熟悉.干脆就自己建立一个. 步骤很简单: 一:下载一个bulidro ...

  2. 转:linux下bwa和samtools的安装与使用

    bwa的安装流程安装本软体总共需要完成以下两个软体的安装工作:1) BWA2) Samtools1.BWA的安装a.下载BWA (download from BWA Source Forge ) ht ...

  3. time()函数,dirname(__FILE__) 的使用总结

    time()函数将返回从1970-1-1 0:0:0到当前时间的秒数.(整型) dirname(__FILE__) php中定义了一个很有用的常数,即 __file__ 这个内定常数是当前php程序的 ...

  4. struts2将servlet对象注入到Action中

    在struts2框架中,可以通过IoC方式将servlet对象注入到Action中,通常需要Action实现以下接口: a. ServletRequestAware: 实现该接口的Action可以直接 ...

  5. css常用效果总结

    1.给input的placeholder设置颜色 .phColor::-webkit-input-placeholder { /* WebKit, Blink, Edge */ color:maroo ...

  6. Alien Dictionary

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  7. C++中的结构体

    http://zhidao.baidu.com/link?url=8OYQSKV9mvSBc6Hkf9NsLQmipSge9VCZDJQGAZZs5PCBQ54UTmK98VRmAklEEAFYu7d ...

  8. 【leetcode】Excel Sheet Column Title

    Excel Sheet Column Title Given a non-zero positive integer, return its corresponding column title as ...

  9. Java for LeetCode 208 Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...

  10. manage account

    #!/bin/bash # #Delete_user - Automates the steps to remove an account # ############################ ...