日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

CPython学习

發布時間:2024/3/24 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CPython学习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

Python有時候太慢,如果手動編譯C或者是C++來寫#include<Python.h>的文件也比較麻煩。
CPython無疑是一個比較好的選擇。

簡介

CPython是特指C語言實現的Python,就是原汁原味的Python。

之所以使用CPython這個詞,是因為Python還有一些其它的實現,比如Jython,就是Java版的Python,還有燒腦的PyPy,使用Python再把Python實現了一遍。

如下是官方對CPython的說明:

CPython is Guido van Rossum’s reference version of the Python computing language. It’s most often called simply “Python”; speakers say “CPython” generally to distinguish it explicitly from other implementations.

案例

先從一個Hello Word開始

  • 創建一個文件helloworld.pyx,內容如下:

    print("Hello world!") #pyx文件是python的c擴展文件,代碼要符合cython的規范,用什么編輯器寫都行。
  • 保存后,創建setup.py文件,內容如下:

    from distutils.core import setupfrom Cython.Build import cythonizesetup(ext_modules = cythonize("helloworld.pyx"))
  • 保存后,進入setup.py所在目錄,并執行編譯命令:

    python setup.py build_ext --inplace

    會輸出如下結果:

    $python setup.py build_ext --inplace
    Compiling helloworld.pyx because it changed.
    [1/1] Cythonizing helloworld.pyx
    /anaconda3/envs/deeplearning/lib/python3.7/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive ‘language_level’ not set, using 2 for now (Py2). This will change in a later release! File: /Users/user/pytorch/NLP學習/learning_2.0/helloworld.pyx
    tree = Parsing.p_module(s, pxd, full_module_name)
    running build_ext
    building ‘helloworld’ extension
    creating build
    creating build/temp.macosx-10.9-x86_64-3.7
    gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/anaconda3/envs/deeplearning/include -arch x86_64 -I/anaconda3/envs/deeplearning/include -arch x86_64 -I/anaconda3/envs/deeplearning/include/python3.7m -c helloworld.c -o build/temp.macosx-10.9-x86_64-3.7/helloworld.o
    gcc -bundle -undefined dynamic_lookup -L/anaconda3/envs/deeplearning/lib -arch x86_64 -L/anaconda3/envs/deeplearning/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.9-x86_64-3.7/helloworld.o -o /Users/user/pytorch/NLP學習/learning_2.0/helloworld.cpython-37m-darwin.so

  • 運行完這個命令后,該目錄下就會生成三個文件:

    build helloworld.pyxhelloworld.c setup.pyhelloworld.cpython-37m-darwin.so
  • 然后創建一個調用文件test.py,內容為:

    import helloworld

    運行返回:

    i$ python test.py Hello world!
  • Fibonacci Function項目

    斐波那契數列:1, 1, 2, 3, 5,… 前兩位為1,之后每個數等于前面兩個數之和。

    創建fib.pyx,內容如下:

    from __future__ import print_functiondef fib(n):a, b = 0, 1while b < n:print(b, end=' ')a, b = b, a+bprint()

    創建setup.py文件,內容如下:

    from distutils.core import setup from Cython.Build import cythonizesetup(ext_modules = cythonize("fib.pyx") )

    通過命令python setup.py build_ext --inplace,生成出來的文件:

    python setup.py build_ext --inplace Compiling fib.pyx because it changed. [1/1] Cythonizing fib.pyx /anaconda3/envs/deeplearning/lib/python3.7/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /Users/user/pytorch/NLP學習/learning_2.0/fib.pyxtree = Parsing.p_module(s, pxd, full_module_name) running build_ext building 'fib' extension gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/anaconda3/envs/deeplearning/include -arch x86_64 -I/anaconda3/envs/deeplearning/include -arch x86_64 -I/anaconda3/envs/deeplearning/include/python3.7m -c fib.c -o build/temp.macosx-10.9-x86_64-3.7/fib.o gcc -bundle -undefined dynamic_lookup -L/anaconda3/envs/deeplearning/lib -arch x86_64 -L/anaconda3/envs/deeplearning/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.9-x86_64-3.7/fib.o -o /Users/user/pytorch/NLP學習/learning_2.0/fib.cpython-37m-darwin.so

    測試test.py:

    import fib fib.fib(100)

    返回:

    $ python test.py 1 1 2 3 5 8 13 21 34 55 89

    總結

    以上是生活随笔為你收集整理的CPython学习的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。