Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Import ฟังก์ชันเดียวจากโมดูล Python (from ... import)

    ใช้ from module import function นำเข้าฟังก์ชันเฉพาะตัว ตัวอย่าง >>> from math import sin >>> sin(0) 0.0 # ใช้ sin() โดยตรง ไม่ต้อง math.sin() Alias >>> from math import cos as cosine >>> cosine(0) 1.0 เปรียบเทียบ StyleCodeใช้งาน Module Importimport math;

  2. คำสั่ง from...import ใน Python (Import Specific)

    from module import name นำเข้าชื่อเฉพาะตัว เข้า Namespace โดยตรง (ไม่ต้อง Prefix) ตัวอย่าง >>> from math import sin >>> sin(0) 0.0 # ไม่ต้องเขียน math.sin() Alias (เปลี่ยนชื่อ) >>> from math import cos as cosine >>> cosine(0) 1.0 Import หลายชื่อ >>>

  3. คำสั่ง from...import * ใน Python (Import All)

    from module import * นำเข้าทุก Public Name จาก Module (ยกเว้น _ prefix และ __all__) ตัวอย่าง >>> from math import * >>> sin(0) 0.0 >>> cos(0) 1.0 >>> pi 3.141592653589793 ข้อเสีย (Anti-Pattern) Namespace Pollution: ชื่อชนกัน ทำให้ Bug ยากหา Implicit Dependenci

  4. หาตำแหน่ง Module ใน Python (__file__, python -v)

    Pure Python Module: ใช้ module.__file__ ตัวอย่าง >>> import mymodule >>> mymodule.__file__ C:/Users/Ayush/mymodule.py Built-in Modules (C Extension) __file__ → .so / .pyd (ไม่ใช่ Source) Windows บางตัวไม่มี __file__ Verbose Mode: Trace Import ทั้งหมด $ python -v script.py # i

  5. PYTHONPATH Environment Variable คืออะไร?

    PYTHONPATH = Environment Variable ระบุ Additional Directories ที่ Python ควรค้นหา Module/Package เมื่อไหร่ต้องตั้ง? มี Custom Library ไม่อยากลง site-packages (Global) Development: ชี้ไป Source Code Local แทน Installed Package Legacy Project ที่ Hardcode Path เมื่อไหร่ ไม่ ควรตั้ง? Standard Inst

  6. ตั้งค่า Environment Variable PYTHONPATH บน Linux (Duplicate)

    ซ้ำกับ ID 1004107980 - เนื้อหาเหมือนกันทุกประการ วิธีตั้งค่า export PYTHONPATH=${PYTHONPATH}:${HOME}/foo # ใส่ใน ~/.bashrc หรือ /etc/environment ข้อควรระวัง Append ด้วย Colon : ไม่แนะนำให้ตั้ง Manual ใช้ Virtualenv แทน

  7. ตั้งค่า Environment Variable PYTHONPATH บน macOS (Duplicate)

    ซ้ำกับ ID 1004107979 - เนื้อหาเหมือนกันทุกประการ วิธีตั้งค่า export PYTHONPATH=${PYTHONPATH}:${HOME}/foo # ใส่ใน ~/.zshrc หรือ ~/.bash_profile ข้อควรระวัง Append ด้วย Colon : ไม่แนะนำให้ตั้ง Manual ใช้ Virtualenv แทน

  8. ตั้งค่า Environment Variable PYTHONPATH บน Windows (Duplicate)

    ซ้ำกับ ID 1004107978 - เนื้อหาเหมือนกันทุกประการ วิธีตั้งค่า (GUI) My Computer → Properties → Advanced System Settings → Environment Variables System Variables → PYTHONPATH → Edit/New Value: C:\Python39;C:\MyProjects\mylibs (คั่นด้วย ;) PowerShell Permanent [Environment]::SetEnvironmentVariable(

  9. ตั้งค่า PYTHONPATH บน Linux

    เหมือน macOS ทุกประการ (Linux/Unish ใช้ Colon : คั่น) Temporary export PYTHONPATH=${PYTHONPATH}:${HOME}/my_project/lib python script.py Permanent # ~/.bashrc, ~/.profile, /etc/environment export PYTHONPATH=${PYTHONPATH}:${HOME}/my_project/lib System-wide (All Users) # /etc/environment PYTHONPAT

  10. ตั้งค่า PYTHONPATH บน macOS

    ตั้ง PYTHONPATH บน macOS/Linux ผ่าน Shell Config Temporary (Current Shell) export PYTHONPATH=${PYTHONPATH}:${HOME}/my_project/lib python script.py Permanent (เพิ่มใน Shell RC File) # ~/.zshrc หรือ ~/.bash_profile หรือ ~/.bashrc export PYTHONPATH=${PYTHONPATH}:${HOME}/my_project/lib แล้ว source ~

  11. ตั้งค่า PYTHONPATH บน Windows

    PYTHONPATH = Environment Variable บอก Python ว่าไปหา Module/Package ที่ไหนเพิ่มเติม วิธีตั้งค่า (GUI) My Computer → Properties → Advanced System Settings → Environment Variables Section System Variables → หา PYTHONPATH → Edit (หรือ New) Value: C:\Python39;C:\MyProjects\mylibs (คั่นด้วย Semicolon ;

  12. Namespace ใน Python คืออะไร? (พื้นฐาน)

    Namespace = ระบบ Mapping จาก ชื่อ (Name) ไปยัง อ็อบเจกต์ (Object) ใช้แก้ปัญหาชื่อชนกัน ลักษณะสำคัญ แต่ละ Package, Module, Class, Function, Method มี Namespace ของตัวเอง สร้างขึ้นตอนเริ่ม Execute, หายตอนจบ Execution คิดเป็น Evaluation Context หรือ Symbol Table ประเภท Namespace Built-in Namespace

  13. Python Namespace และ Variable Scope (LEGB Rule)

    Namespace = Mapping ชื่อ → Object | Scope = พื้นที่ที่ชื่อมองเห็นได้ LEGB Rule (ลำดับการค้นหา) L - Local: ภายใน Function/Lambda (Assignment, Parameter) E - Enclosing: ฟังก์ชันภายนอก (Nested Functions) เรียกจากในออกไป G - Global (Module): Top-level Module, หรือ global Statement B - Built-in: open,

  14. ความต่าง dir(), globals(), locals(), vars() ใน Python

    4 ฟังก์ชันสำหรับ Inspect Namespace/Scope globals() vs locals() globals() → Dict ของ Global Namespace (Module Level) locals() → Dict ของ Local Namespace (Function Scope) ที่ Global Scope: globals() is locals() == True ตัวอย่างใน Function def fun(): var = 123 print(Locals:, locals()) # {

  15. ฟังก์ชัน reload() ใน Python ทำอะไร?

    reload(module) โหลดโมดูลซ้ำ (Re-execute Module Code) ใช้ตอน Develop Python 2 >>> import mymodule >>> reload(mymodule) Python 3 >>> import importlib >>> importlib.reload(mymodule) รายละเอียดการทำงาน Re-compile Source Code Re-execute Module-level Code Update M

  16. Package ใน Python คืออะไร? (Module vs Package)

    Module = ไฟล์ .py เดียว (ชื่อ = ชื่อไฟล์ ไม่รวม .py) Package = โฟลเดอร์ที่มีโมดูลหลายตัว + ไฟล์ __init__.py โครงสร้าง mypackage/ # Package __init__.py # ทำให้เป็น Package (อาจว่างได้) module1.py # Module module2.py # Module subpackage/ # Nested Packa

  17. เช็ค Version โมดูล Python (pip freeze, __version__)

    หลายวิธีดู Version โมดูลที่ติดตั้งไว้ 1. Command Line: pip freeze $ pip freeze asn1crypto==0.22.0 astroid==1.5.2 attrs==16.3.0 ... 2. Filter หา Package เฉพาะ # Linux/macOS $ pip freeze | grep PyMySQL PyMySQL==0.7.11 # Windows PowerShell $ pip freeze | findstr PyMySQL PyMySQL==0.7.11 3. ใน Pyth

  18. โมดูล Python แปลง PDF เป็น Text (PDFMiner, PyPDF)

    2 Library หลักสำหรับ Extract Text จาก PDF 1. PDFMiner (Powerful, Complex) import sys from io import StringIO from pdfminer.pdfpage import PDFPage from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter from pdfminer.layout import LAParams from pdfminer.converter import TextConverter

  19. ดึง Path ของ Python Module (__file__ attribute)

    โมดูล Pure Python: Attribute __file__ เก็บ Path ของไฟล์ Source ตัวอย่าง >>> import mymodule >>> mymodule.__file__ C:/Users/Ayush/mymodule.py Built-in Modules ชี้ไป Shared Library (.so Linux/macOS, .pyd Windows) Windows บางตัวไม่มี __file__ ไม่ใช่ Source Code คำสั่ง python -v

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:16/451  20-คอมพิวเตอร์/Page Goto:1 10 11 12 13 14 15 16 17 18 19 20 21 22