码迷,mamicode.com
首页 > 编程语言 > 详细

python元类编程

时间:2017-04-17 12:59:28      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:type   ase   代码   war   bool   hello   sig   create   *args   

什么叫元类?   年轻人先不要在意这些细节、我们一步一步的来!

 

001、

    oop的世界里有一句话 “万物皆对象” 

class Person(object):
    name=None

if __name__=="__main__":
    i=123
    s="hello world"
    p=Person()
    print(type(i)) #<class ‘int‘>
    print(type(s)) #<class ‘str‘>
    print(type(p)) #<class ‘__main__.Person‘>

    print(type(int)) #<class ‘type‘>
    print(type(str)) #<class ‘type‘>
    print(type(Person)) #<class ‘type‘>

我们来说一下上面代码的意思

    1、前三句我们可以看出:i 是int 类的实例,s是str类的实例,p是Person类的实例;#我下面要说的话,可以让你感觉到不适

    2、后三句我们可以看出:int,str,Person 这些类事实上它们都只是type类的一个实例

 

002、

    我真的没有逗你、type它真的是一个类呀!不信你help(type)看一下

class type(object)
 |  type(object_or_name, bases, dict)
 |  type(object) -> the objects type
 |  type(name, bases, dict) -> a new type
 |  
 |  Methods defined here:
 |  
 |  __call__(self, /, *args, **kwargs)
 |      Call self as a function.
 |  
 |  __delattr__(self, name, /)
 |      Implement delattr(self, name).
 |  
 |  __dir__(...)
 |      __dir__() -> list
 |      specialized __dir__ implementation for types
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __instancecheck__(...)
 |      __instancecheck__() -> bool
 |      check if an object is an instance
 |  
 |  __new__(*args, **kwargs)
 |      Create and return a new object.  See help(type) for accurate signature.

 

 

 

    

 

 

 

 

 

----

python元类编程

标签:type   ase   代码   war   bool   hello   sig   create   *args   

原文地址:http://www.cnblogs.com/JiangLe/p/6722166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!