python class的创建
def f():
class a():
a=5
def f2():
pass Disassembly of f:
14 0 LOAD_CONST 1 ('a')
3 LOAD_CONST 3 (())
6 LOAD_CONST 2 (<code object a at 0133DB18, file "<module1>", line 14>)
9 MAKE_FUNCTION 0
12 CALL_FUNCTION 0
15 BUILD_CLASS
16 STORE_FAST 0 (a)
19 LOAD_CONST 0 (None)
22 RETURN_VALUE
在python opcod.h在 #define BUILD_CLASS 89 ceval.h创建类的伪指令:
case BUILD_CLASS:
u = TOP();
v = SECOND();
w = THIRD();
STACKADJ(-);
x = build_class(u, v, w);#调用到此函数
SET_TOP(x);
Py_DECREF(u);
Py_DECREF(v);
Py_DECREF(w);
break;
build_class(PyObject *methods, PyObject *bases, PyObject *name)
{
PyObject *metaclass = NULL, *result, *base; if (PyDict_Check(methods))
metaclass = PyDict_GetItemString(methods, "__metaclass__");#在词典中找元类
if (metaclass != NULL)
Py_INCREF(metaclass);
else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > ) {
base = PyTuple_GET_ITEM(bases, );
metaclass = PyObject_GetAttrString(base, "__class__");
if (metaclass == NULL) {
PyErr_Clear();
metaclass = (PyObject *)base->ob_type;
Py_INCREF(metaclass);
}
}
else {
PyObject *g = PyEval_GetGlobals();
if (g != NULL && PyDict_Check(g))
metaclass = PyDict_GetItemString(g, "__metaclass__");
if (metaclass == NULL)
metaclass = (PyObject *) &PyClass_Type;
Py_INCREF(metaclass);
}
result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
NULL);
Py_DECREF(metaclass);
if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
/* A type error here likely means that the user passed
in a base that was not a class (such the random module
instead of the random.random type). Help them out with
by augmenting the error message with more information.*/ PyObject *ptype, *pvalue, *ptraceback; PyErr_Fetch(&ptype, &pvalue, &ptraceback);
if (PyString_Check(pvalue)) {
PyObject *newmsg;
newmsg = PyString_FromFormat(
"Error when calling the metaclass bases\n"
" %s",
PyString_AS_STRING(pvalue));
if (newmsg != NULL) {
Py_DECREF(pvalue);
pvalue = newmsg;
}
}
PyErr_Restore(ptype, pvalue, ptraceback);
}
return result;
}
#一般类继承自object,object的__class__属性是type,所有创建类时会调用到type的tp_call
PyObject *
PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
{
ternaryfunc call; if ((call = func->ob_type->tp_call) != NULL) {
PyObject *result;
if (Py_EnterRecursiveCall(" while calling a Python object"))
return NULL;
result = (*call)(func, arg, kw);
Py_LeaveRecursiveCall();
if (result == NULL && !PyErr_Occurred())
PyErr_SetString(
PyExc_SystemError,
"NULL result without error in PyObject_Call");
return result;
}
PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
func->ob_type->tp_name);
return NULL;
}
#ty_call又会调用到tp_new()函数
type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *obj; if (type->tp_new == NULL) {
PyErr_Format(PyExc_TypeError,
"cannot create '%.100s' instances",
type->tp_name);
return NULL;
} obj = type->tp_new(type, args, kwds);
if (obj != NULL) {
/* Ugly exception: when the call was type(something),
don't call tp_init on the result. */
if (type == &PyType_Type &&
PyTuple_Check(args) && PyTuple_GET_SIZE(args) == &&
(kwds == NULL ||
(PyDict_Check(kwds) && PyDict_Size(kwds) == )))
return obj;
/* If the returned object is not an instance of type,
it won't be initialized. */
if (!PyType_IsSubtype(obj->ob_type, type))
return obj;
type = obj->ob_type;
if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
type->tp_init != NULL &&
type->tp_init(obj, args, kwds) < ) {
Py_DECREF(obj);
obj = NULL;
}
}
return obj;
}
tp_new函数完成创建一个类的创建细节
再调用tp_ready
python class的创建的更多相关文章
- python 读写、创建 文件
python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...
- 使用Python Shapefile Library创建和编辑Shapefile文件
介绍 shapefile是GIS中非常重要的一种数据类型,在ArcGIS中被称为要素类(Feature Classes),主要包括点(point).线(polyline)和多边形(polygon).P ...
- python 读写、创建 文件的方法(必看)
python 读写.创建 文件的方法(必看) 更新时间:2016年09月12日 10:26:41 投稿:jingxian 我要评论下面小编就为大家带来一篇python 读写.创建 文件的方法(必看). ...
- python 一次创建多级目录
python 一次创建多级目录沙漠骆驼:qq音乐import osos.mkdirs('/home/user/app')
- 使用python type动态创建类
使用python type动态创建类 X = type('X', (object,), dict(a=1)) # 产生一个新的类型 X 和下列方法class X(object): a = 1效 ...
- python之multiprocessing创建进程
python的multiprocessing模块是用来创建多进程的,下面对multiprocessing总结一下使用记录. multiprocessing创建多进程在windows和linux系统下的 ...
- python读写、创建文件、文件夹等等
python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...
- Python命令行创建虚拟环境
Python命令行创建虚拟环境 安装virtualenv 启动命令行,执行命令pip install -U virtualenv 创建一个新的虚拟环境 执行命令python -m virtualenv ...
- python进阶ing——创建第一个Tornado应用
python进阶ing——创建第一个Tornado应用 分类: Python2013-06-02 23:02 1725人阅读 评论(2) 收藏 举报 pythonTornado 每天在群里跟很多群友讨 ...
- Python测试开发-创建模态框及保存数据
Python测试开发-创建模态框及保存数据 原创: fin 测试开发社区 前天 什么是模态框? 模态框是指的在覆盖在父窗体上的子窗体.可用来做交互,我们经常会看到模态框用来登录.确定等等,到底是怎 ...
随机推荐
- LAMP架构上(一)
第十七课LAMP架构上(一) 目录 一.LAMP架构介绍 二.MySQL.MariaDB介绍 三.MySQL安装 四.MariaDB安装 五.Apache安装 六.安装PHP5 七.安装PHP7 八. ...
- BiLstm原理
Lstm这里就不说了,直接说Bilstm. 前向的LSTM与后向的LSTM结合成BiLSTM.比如,我们对“我爱中国”这句话进行编码,模型如图所示. 前向的依次输入“我”,“爱”,“中国”得到三个向量 ...
- javascript 统计字符串中每个字符出现的次数
var str = "abdcadfasfdbadfafdasdfasyweroweurowqrewqrwqrebwqrewqrejwq;;"; // console.log(nu ...
- 解决启动vs2010 未能找到自动配置的设置文件
今天室友把固态拆掉,重新安上.打开vs2010出现 打开项目,出现 找了许多方法都无效. 发现c:user\Administer\documents\下,vs2010 .vs webset 那些文件点 ...
- asp.net button控件 使用JS的 disabled
今天想用JS禁用asp.net的button控件,查了好久,都是一行代码.... document.getElementById("Button1").disabled ...
- Spring中的@Bean注解、@Configuration注解、@Value
1.首先是注册bean类:@Component.@Responsitory.@Controller.@Service.Configuration这些注解是把要实例化的对象装化成一个bean,放到Ioc ...
- day 12 名称空间和闭包函数
函数嵌套 按照函数的两个阶段分为: 嵌套调用:在一个函数内部调用另一个函数 嵌套定义:在一个函数内部定义另一个函数 名称空间(namespace):存储名字的内存区域 名称空间的分类: 内置名称空间: ...
- SpringBoot的学习【2.分析HelloWorld的依赖】
1.分析引入的父类项目 引入的父类如下. 点击进去发现又引入了一个父类 点击进去发现是spring的版本控制和相应的依赖. so~这就是springboot的终极大包.想用springboot开发的时 ...
- MySQL数据库内置加密函数总结
首先,我认识的加密函数有以下几个: password(plainText):旧版(OLD_PASSWORD())加密后长度16位,新版41位select length(password("1 ...
- 大道至简第一章读后感JAVA伪代码形式
观看了<大道至简>第一章后,从愚公移山的故事中可以发现愚公移山其实可以看作是一个一个项目,下面用JAVA伪代码形式编写: Import java(愚公移山) //愚公移山 public c ...