cpython

how does Cpython implement its type Objects, i.e. type's type is always type?

跟風遠走 提交于 2019-12-03 04:58:06
I understand that everything in python is an Object and that the 'type' (or class) of these object is 'type'. Plus the type of type is also type itself. (as explained nicely here ) What I do not understand is how is this circular reference implemented? So i looked here . To quote the portion which might explain what I am looking for: PyTypeObject* PyObject.ob_type This is the type’s type, in other words its metatype. It is initialized by the argument to the PyObject_HEAD_INIT macro, and its value should normally be &PyType_Type. However, for dynamically loadable extension modules that must be

Why is Python 3 is considerably slower than Python 2? [duplicate]

∥☆過路亽.° 提交于 2019-12-03 04:09:56
问题 This question already has answers here : Is there a reason Python 3 enumerates slower than Python 2? (2 answers) Closed 4 years ago . I've been trying to understand why Python 3 is actually taking much time compared with Python 2 in certain situations, below are few cases I've verified from python 3.4 to python 2.7. Note: I've gone through some of the questions like Why is there no xrange function in Python3? and loop in python3 much slower than python2 and Same code slower in Python3 as

C++ vector to Python 3.3

自闭症网瘾萝莉.ら 提交于 2019-12-03 03:11:37
I would like to get a python list, say, [1,2,3,4] , from a C++ script. I wrote the C++ script, which returns a vector. How to connect the ends without SWIG/SIP/Cython/and others? Could it be easier to just compile the C++ to an .exe or elf file and then call from command line, have the .exe create a .txt containing a vector and read it in with python? My point is, I only need a really small function from C++ to do the heavy calculations on huge data. What would be the least painful and shortest method to do just this? EDIT: To give an example. Python will give a filename string to C++ ("foo

Why are some float < integer comparisons four times slower than others?

放肆的年华 提交于 2019-12-03 00:04:29
问题 When comparing floats to integers, some pairs of values take much longer to be evaluated than other values of a similar magnitude. For example: >>> import timeit >>> timeit.timeit("562949953420000.7 < 562949953421000") # run 1 million times 0.5387085462592742 But if the float or integer is made smaller or larger by a certain amount, the comparison runs much more quickly: >>> timeit.timeit("562949953420000.7 < 562949953422000") # integer increased by 1000 0.1481498428446173 >>> timeit.timeit(

What are some strategies to write python code that works in CPython, Jython and IronPython

我与影子孤独终老i 提交于 2019-12-02 20:45:59
Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation? If you do find you need to write unique code for an environment, use pythons import mymodule_jython as mymodule import mymodule_cpython as mymodule have this stuff in a simple module (''module_importer''?) and write your code like this: from module_importer import mymodule This way, all you need to do is alter module_importer.py per platform. @Daren Thomas: I

uWSGI部署django应用

◇◆丶佛笑我妖孽 提交于 2019-12-02 19:59:10
django-admin startproject demo demo项目的目录结构 demo/ ├── db.sqlite3 ├── demo │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-36.pyc │ │ ├── settings.cpython-36.pyc │ │ ├── urls.cpython-36.pyc │ │ └── wsgi.cpython-36.pyc │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── manage.py └── requirements.txt 1. 安装相关工具和依赖 [root@uwsgi ~]# yum -y install git wget httpd vim # 安装相关工具 [root@uwsgi ~]# yum -y install gcc zlib* openssl-devel # 安装编译工具和依赖库 2. 安装 Python 环境 rhel系列无法通过yum直接安装python3,需要源码编译安装 [root@uwsgi ~]# pwd # 查看当前目录 /root [root@uwsgi ~]# wget https://www.python.org/ftp/python/3.6.5

CPython - How to create and add a method attribute to an object with __dict__?

社会主义新天地 提交于 2019-12-02 18:21:08
问题 This can be a tricky question... In short I am creating and adding a method as follows: static PyObject *ret_arg(PyBVHTree *self, PyObject *arg) { /* just to demonstrate */ return arg; } static PyMethodDef my_meth = {"ret_arg", (PyCFunction)ret_arg, METH_O, 0}; ... PyObject* func = PyCFunction_New(&my_meth, my_object); Py_DECREF(my_object); PyObject_SetAttrString(my_object, "ret_arg", func); Py_DECREF(func); return my_object; } It works! But having some problems :( eg.: If I del my_object.

Why are some float < integer comparisons four times slower than others?

本小妞迷上赌 提交于 2019-12-02 13:50:55
When comparing floats to integers, some pairs of values take much longer to be evaluated than other values of a similar magnitude. For example: >>> import timeit >>> timeit.timeit("562949953420000.7 < 562949953421000") # run 1 million times 0.5387085462592742 But if the float or integer is made smaller or larger by a certain amount, the comparison runs much more quickly: >>> timeit.timeit("562949953420000.7 < 562949953422000") # integer increased by 1000 0.1481498428446173 >>> timeit.timeit("562949953423001.8 < 562949953421000") # float increased by 3001.1 0.1459577925548956 Changing the

CPython - How to create and add a method attribute to an object with __dict__?

会有一股神秘感。 提交于 2019-12-02 08:50:48
This can be a tricky question... In short I am creating and adding a method as follows: static PyObject *ret_arg(PyBVHTree *self, PyObject *arg) { /* just to demonstrate */ return arg; } static PyMethodDef my_meth = {"ret_arg", (PyCFunction)ret_arg, METH_O, 0}; ... PyObject* func = PyCFunction_New(&my_meth, my_object); Py_DECREF(my_object); PyObject_SetAttrString(my_object, "ret_arg", func); Py_DECREF(func); return my_object; } It works! But having some problems :( eg.: If I del my_object. And use the new method, CRASH . When I close the program with python. Error: EXCEPTION_ACCESS_VIOLATION.

Possible mixed indentation in Python?

跟風遠走 提交于 2019-12-01 22:11:29
问题 Looking at this question, I tried OP's the code on my machine. Here are a text version and a screenshot: What just happened? This supposed to be a square function, and it is implemented correctly. To be sure, I copy-pasted the code, and tried it again: Well, I can't see any difference between these versions of square , but only the latter works. The only reason I can think of is that I may have mixed tabs and spaces, so the return statement is actually indented, and so the loop is executed