python的type class
在python中,用户定义的class是一个PyTypeObject ( XXX_Type)对象。
#PyType_Type是一切类的基类,这是一个全局数据
PyTypeObject PyType_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, )
"type", /* tp_name */
sizeof(PyHeapTypeObject), /* tp_basicsize */
sizeof(PyMemberDef), /* tp_itemsize */
(destructor)type_dealloc, /* tp_dealloc */
, /* tp_print */
, /* tp_getattr */
, /* tp_setattr */
, /* tp_compare */
(reprfunc)type_repr, /* tp_repr */
, /* tp_as_number */
, /* tp_as_sequence */
, /* tp_as_mapping */
(hashfunc)_Py_HashPointer, /* tp_hash */
(ternaryfunc)type_call, /* tp_call */
, /* tp_str */
(getattrofunc)type_getattro, /* tp_getattro */
(setattrofunc)type_setattro, /* tp_setattro */
, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */
type_doc, /* tp_doc */
(traverseproc)type_traverse, /* tp_traverse */
(inquiry)type_clear, /* tp_clear */
type_richcompare, /* tp_richcompare */
offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
, /* tp_iter */
, /* tp_iternext */
type_methods, /* tp_methods */
type_members, /* tp_members */
type_getsets, /* tp_getset */
, /* tp_base */
, /* tp_dict */
, /* tp_descr_get */
, /* tp_descr_set */
offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
type_init, /* tp_init */
, /* tp_alloc */
type_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
(inquiry)type_is_gc, /* tp_is_gc */
};
list是一个具体的type:
PyTypeObject PyList_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, )
"list",
sizeof(PyListObject),
,
(destructor)list_dealloc, /* tp_dealloc */
(printfunc)list_print, /* tp_print */
, /* tp_getattr */
, /* tp_setattr */
, /* tp_compare */
(reprfunc)list_repr, /* tp_repr */
, /* tp_as_number */
&list_as_sequence, /* tp_as_sequence */
&list_as_mapping, /* tp_as_mapping */
(hashfunc)PyObject_HashNotImplemented, /* tp_hash */
, /* tp_call */
, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
, /* tp_setattro */
, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
list_doc, /* tp_doc */
(traverseproc)list_traverse, /* tp_traverse */
(inquiry)list_clear, /* tp_clear */
list_richcompare, /* tp_richcompare */
, /* tp_weaklistoffset */
list_iter, /* tp_iter */
, /* tp_iternext */
list_methods, /* tp_methods */
, /* tp_members */
, /* tp_getset */
, /* tp_base */
, /* tp_dict */
, /* tp_descr_get */
, /* tp_descr_set */
, /* tp_dictoffset */
(initproc)list_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */
PyObject_GC_Del, /* tp_free */
};
typedef struct {
PyObject_VAR_HEAD
/* Vector of pointers to list elements. list[0] is ob_item[0], etc. */
PyObject **ob_item; /* ob_item contains space for 'allocated' elements. The number
* currently in use is ob_size.
* Invariants:
* 0 <= ob_size <= allocated
* len(list) == ob_size
* ob_item == NULL implies ob_size == allocated == 0
* list.sort() temporarily sets allocated to -1 to detect mutations.
*
* Items must normally not be NULL, except during construction when
* the list is not yet visible outside the function that builds it.
*/
Py_ssize_t allocated;
} PyListObject;
list的实例对象是PyListObject,保存了list的数据,由list对象的head可以知道他的PyList_Type,从而可以调用PyList_Type的方法。
PyTypeObject PyDict_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, )
"dict",
sizeof(PyDictObject),
,
(destructor)dict_dealloc, /* tp_dealloc */
(printfunc)dict_print, /* tp_print */
, /* tp_getattr */
, /* tp_setattr */
(cmpfunc)dict_compare, /* tp_compare */
(reprfunc)dict_repr, /* tp_repr */
, /* tp_as_number */
&dict_as_sequence, /* tp_as_sequence */
&dict_as_mapping, /* tp_as_mapping */
(hashfunc)PyObject_HashNotImplemented, /* tp_hash */
, /* tp_call */
, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
, /* tp_setattro */
, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
dictionary_doc, /* tp_doc */
dict_traverse, /* tp_traverse */
dict_tp_clear, /* tp_clear */
dict_richcompare, /* tp_richcompare */
, /* tp_weaklistoffset */
(getiterfunc)dict_iter, /* tp_iter */
, /* tp_iternext */
mapp_methods, /* tp_methods */
, /* tp_members */
, /* tp_getset */
, /* tp_base */
, /* tp_dict */
, /* tp_descr_get */
, /* tp_descr_set */
, /* tp_dictoffset */
dict_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
dict_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
};
PyTypeObject PyBaseObject_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, )
"object", /* tp_name */
sizeof(PyObject), /* tp_basicsize */
, /* tp_itemsize */
object_dealloc, /* tp_dealloc */
, /* tp_print */
, /* tp_getattr */
, /* tp_setattr */
, /* tp_compare */
object_repr, /* tp_repr */
, /* tp_as_number */
, /* tp_as_sequence */
, /* tp_as_mapping */
(hashfunc)_Py_HashPointer, /* tp_hash */
, /* tp_call */
object_str, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
PyObject_GenericSetAttr, /* tp_setattro */
, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
PyDoc_STR("The most base type"), /* tp_doc */
, /* tp_traverse */
, /* tp_clear */
, /* tp_richcompare */
, /* tp_weaklistoffset */
, /* tp_iter */
, /* tp_iternext */
object_methods, /* tp_methods */
, /* tp_members */
object_getsets, /* tp_getset */
, /* tp_base */
, /* tp_dict */
, /* tp_descr_get */
, /* tp_descr_set */
, /* tp_dictoffset */
object_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
object_new, /* tp_new */
PyObject_Del, /* tp_free */
};
o=object(),object也是一种类。tp_dict有什么作用?类的__dict__是用来保存实例对象的。
>>> class t(object):
name='hai'
age=41
def __init__(self,a,b):
self.m1=a
self.m2=b
def func2():
pass >>> oo=t(5,7)
>>> oo.__dict__
{'m1': 5, 'm2': 7}
>>> t.__dict__
dict_proxy({'func2': <function func2 at 0x013AF570>, '__module__': '__main__', 'name': 'hai', 'age': 41, '__dict__': <attribute '__dict__' of 't' objects>, '__weakref__': <attribute '__weakref__' of 't' objects>, '__doc__': None, '__init__': <function __init__ at 0x013AF530>})
>>>
>>> class t(object):
name='hai'
age=41 >>> o=t()
>>> dir(o)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name']
>>> o.__dict__
{}
>>> dir(t)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name']
>>> type(t)
<type 'type'>
>>> o.__class__
<class '__main__.t'>
>>> t.__class__
<type 'type'>
>>>
>>> class t(object):
name='hai'
age=41
def func1():
pass
def func2():
pass >>> dir(t)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'func1', 'func2', 'name']
>>> o=t()
>>> dir(o)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'func1', 'func2', 'name']
>>>
python的type class的更多相关文章
- Python中type与Object的区别
Python中type与Object的区别 在查看了Python的API后,总算明白了.现在总结如下: 先来看object的说明: Python中关于object的说明很少,甚至只有一句话: clas ...
- python之type函数
python 的type 函数 的介绍的 下面就是此函数的参数 三个参数的意义 '''type(class_name, base_class_tuple, attribute_dict)cla ...
- Python中type的用法
目录 描述 语法 用法 type和isinstance Type和Object 描述 python的 type 函数有两个用法,当只有一个参数的时候,返回对象的类型.当有三个参数的时候返回一个类对象. ...
- Python中type()详解:动态创建类
众所周知: type()函数可以查看变量的类型: 先看一个简单的列子来看一下type查看变量类型 class Animal(): pass a=Animal() print(type(a)) prin ...
- python的type和object
在python中一切皆对象,这是个用python的人都知道的概念,以int举例,比如a=2,type下: 发现他的type是int,在python中type就是类,所以a是类int的一个对象,实例是类 ...
- python 内建函数 type() 和 isinstance() 介绍
Python 不支持方法或函数重载, 因此你必须自己保证调用的就是你想要的函数或对象.一个名字里究竟保存的是什么?相当多,尤其是这是一个类型的名字时.确认接收到的类型对象的身份有很多时候都是很有用的. ...
- Python中type和object
type 所有类是type生成的 a = 1 b = "abc" print("type a:{}".format(type(a))) print(" ...
- python 用type()创建类
type()可以查看一个类型,也可以查看变量的类型 class Hello1(object): def hello(self, name = 'world'): print('Hello, %s' % ...
- python: "TypeError: 'type' object is not subscriptable"
目前stackoverflow找到两种情况的解决办法: 1.TypeError: 'type' object is not subscriptable when indexing in to a di ...
随机推荐
- Map集合利用比较器Comparator根据Key和Value的排序
TreeMap排序 根据Key进行排序 Map的根据key排序需要用到TreeMap对象,因为它是默认按照升序进行输出的,可以使用比较器compareTo对它进行降序排序,Comparator可以对集 ...
- c语言中printf()函数中的参数计算顺序
今天看到了一个关于printf()函数计算顺序的问题,首先看一个例子: #include<stdio.h> int main() { printf("%d---%d---%d&q ...
- Hadoop 新增删除节点
1 新增Data节点 1.1 修改/etc/hosts,增加datanode的ip 1.2 在新增加的节点启动服务 hadoop-daemon.sh start datanode yarn-daemo ...
- js 取一个对象的长度,取出来的是undefined,自己写的一个计算长度的函数解决了。
收藏 牙膏儿 发表于 3年前 阅读 13085 收藏 7 点赞 1 评论 1 [粉丝福利]-<web 前端基础到实战系列课程>免费在线直播教学>>> 昨晚写一段代码, ...
- 给VMware下的Linux扩容磁盘空间到根分区(以centos7.0为例)
一.扩展VMWare硬盘空间 关闭Vmware 的 Linux系统,这样,才能在VMWare菜单中设置: VM -> Settings... -> Hardware -> Hard ...
- [UE4]扔枪后捡枪:Get Overlapping Actors
一.搜索碰撞体是否碰撞到枪,使用Get Overlapping Actors,只搜索Class Filter设置的对象类型.Get Overlapping Actors没有Class Filter参数 ...
- [UE4]根据时间、速度进行插值:Finterp to Constant
一般在“Tick”事件中使用: Current:当前值 Target:期望的目标值 Delta Time:时间变化值. Interp Speed:插值速度 返回值:从“当前值”过渡到“期望的目标值”的 ...
- 通过注解实现一个简易的Spring mvc框架
1.首先我们来搭建架构,就建一个普通的javaweb项目就OK了,具体目录如下: 对于小白来说可以细看后面web.xml的配置,对javaweb有点研究可以忽略而过后面的web.xml配置. 2.先上 ...
- PostgreSQL 一主多从(多副本,强同步)简明手册 - 配置、压测、监控、切换、防脑裂、修复、0丢失 - 珍藏级
参考来源: https://github.com/digoal/blog/blob/master/201803/20180326_01.md#postgresql-一主多从多副本强同步简明手册---配 ...
- 使用docker搭建redis主从模式
前期准备: 本地Linux版本:CentOS Linux release 7.5.1804 (Core)Docker版本:Docker version 1.13.1, build dded712/1. ...