Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Garbage Collection ใน Python ทำงานอย่างไร

    Python ลบออบเจ็กต์ที่ไม่ต้องการ (Built-in Types หรือ Class Instances) อัตโนมัติ เพื่อ釋放 Memory กระบวนการที่ Python ระงับและคืนบล็อก Memory ที่ไม่ได้ใช้แล้วเป็นระยะ เรียกว่า Garbage Collection Garbage Collector ของ Python ทำงานระหว่างโปรแกรมรัน และถูก Trigger เมื่อ Reference Count ของออบเจ็กต์ถึงศูน

  2. Class Inheritance ใน Python ทำงานอย่างไร

    Inheritance ในคลาส แทนที่จะนิยามคลาสใหม่ทั้งหมด สามารถสร้างคลาสโดยสืบทอดจากคลาสที่มีอยู่แล้ว โดยระบุ Parent Class ในวงเล็บหลังชื่อคลาสใหม่ Child Class สืบทอด Attributes ของ Parent Class และใช้ได้เหมือนนิยามใน Child Class เอง Child Class ยัง Override Data Members และ Methods ของ Parent ได้อีกด้วย S

  3. ฟังก์ชัน issubclass() ใน Python ทำงานอย่างไร

    สามารถสร้างคลาสสืบทอดจากหลาย Parent Classes ได้: class A: pass class B: pass class C(A, B): pass ใช้ issubclass() ตรวจสอบความสัมพันธ์ระหว่างคลาส: issubclass(sub, sup) Boolean Function คืน True หาก Subclass sub เป็น Subclass จริงของ Superclass sup

  4. ฟังก์ชัน isinstance() ใน Python ทำงานอย่างไร

    สามารถสร้างคลาสสืบทอดจากหลาย Parent Classes ได้: class A: pass class B: pass class C(A, B): pass ใช้ isinstance() ตรวจสอบความสัมพันธ์ระหว่างคลาสและอินสแตนซ์: isinstance(obj, Class) Boolean Function คืน True หาก obj เป็น Instance ของ Class หรือเป็น Instance ของ Subclass ของ Clas

  5. วิธีตรวจสอบว่าคลาสเป็น Subclass ของ Super Class ที่กำหนดใน Python

    มีคลาส A และ B นิยามดังนี้: class A(object): pass class B(A): pass B สามารถพิสูจน์ว่าเป็น Subclass ของ A ได้ 2 วิธี: class A(object): pass class B(A): pass print(issubclass(B, A)) # ใช้ issubclass() ตรวจสอบ print(B.__bases__) # ดู Base Classes ของ B ผลลัพธ์: True (<class __main__.A>,

  6. วิธีตรวจสอบว่า A เป็น Superclass ของ B ใน Python

    มีคลาส A และ B นิยามดังนี้: class A(object): pass class B(A): pass ตัวอย่าง: 2 วิธีพิสูจน์ A เป็น Superclass ของ B class A(object): pass class B(A): pass print(issubclass(B, A)) # ใช้ issubclass() ตรวจสอบ B เป็น subclass ของ A print(B.__bases__) # ดู Base Classes (Superclasses) ของ B ผลลัพ

  7. วิธีตรวจสอบว่า Class Attribute ถูกนิยามหรือสืบทอดใน Python

    โค้ดด้านล่างแสดงว่าแอตทริบิวต์ foo ถูกนิยามหรือสืบทอดในคลาส A และ B ตัวอย่าง not defined in B print(hasattr(B, foo)) # True (derived/inherited) ผลลัพธ์ {__module__: __main__, foo: 1, __doc__: None} True {__module__: __main__, __doc__: None} True ใช้ __dict__ ตรวจสอบว่านิยามในคลาสนั้นจริงหรือสืบ

  8. บทนำ Classes และ Inheritance ใน Python

    Object-Oriented Programming สร้างรูปแบบโค้ดที่นำกลับมาใช้ซ้ำได้ เพื่อป้องกัน Code Redundancy วิธีหนึ่งคือ Inheritance (การสืบทอด) เมื่อ Subclass ใช้โค้ดจาก Base Class Inheritance คือเมื่อคลาสหนึ่งใช้โค้ดที่เขียนในอีกคลาสหนึ่ง Child Classes หรือ Subclasses สืบทอด Methods และ Variables จาก Parent Cl

  9. วิธีสร้าง Subclass จาก Super Class ใน Python

    ใช้ super() Built-in Function ซึ่งเป็นวิธีที่ดีกว่าเล็กน้อยในการเรียก Parent Class เพื่อ Initialization ตัวอย่าง # เรียก Parent Class โดยตรง class MySubClass(MySuperClass): def __init__(self): MySuperClass.__init__(self) # ใช้ super() (แนะนำ) class MySubClassBetter(MySuperClass): d

  10. วิธีใช้ Method Overloading ใน Python

    Method Overloading หมายถึงการมี 2 เมธอดชื่อเดียวกัน Python ไม่สามารถ มี 2 เมธอดชื่อเดียวกันได้ และ ไม่จำเป็น ต้องมี ใน Python Method Overloading ไม่เป็นไปได้ หากต้องการเข้าถึงฟังก์ชันเดียวกันแต่คุณสมบัติต่างกัน ควรใช้ Method Overriding แทน

  11. วิธี Override เมธอดคลาสใน Python

    Overriding คือคุณสมบัติของคลาสในการเปลี่ยน Implementation ของเมธอดที่ Base Class มอบให้ Overriding สำคัญมากใน OOP ทำให้ Inheritance ใช้พลังเต็มที่ ด้วย Method Overriding คลาสอาจ คัดลอก คลาสอื่น หลีกเลี่ยงโค้ดซ้ำ และปรับปรุง/ปรับแต่งบางส่วน Method Overriding จึงเป็นส่วนหนึ่งของกลไก Inheritance ใน P

  12. Constructor __init__ ใน Python ทำงานอย่างไร

    __init__ __init__ เป็น Reserved Method ในคลาส Python เรียกว่า Constructor ในแนวคิด OOP เรียกเมื่อสร้างออบเจ็กต์จากคลาส ให้คลาส Initialize Attributes ได้ การใช้งาน สมมติสร้างคลาส Car มี Attributes: color, model, speed และ Methods: start, accelerate, change_gear ตัวอย่าง class Car(object): def _

  13. Destructor __del__() ใน Python ทำงานอย่างไร

    __del__() เป็น Destructor Method เรียกเมื่อออบเจ็กต์ถูก Garbage Collected ซึ่งเกิดขึ้นหลังจาก Reference ทั้งหมดไปยังออบเจ็กต์ถูกลบแล้ว กรณีง่ายๆ อาจเกิดทันทีหลัง del x หรือจบฟังก์ชัน (ถ้า x เป็น Local Variable) โดยเฉพาะถ้าไม่มี Circular Reference CPython (Standard Implementation) จะ Garbage Collect

  14. ฟังก์ชัน repr() ใน Python OOP ทำอะไร

    เอกสาร Python ระบุว่า __repr__() คำนวณ Official String Representation ของออบเจ็กต์ ฟังก์ชัน repr() ใช้ __repr__() แสดงออบเจ็กต์ __repr__() คืน Printable Representation หนึ่งในวิธีที่สร้างออบเจ็กต์นั้นได้ __repr__() เหมาะสำหรับ Developer ส่วน __str__() สำหรับ End User ตัวอย่าง class Point: def _

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

    เมธอด __str__ __str__ เป็น Special Method คล้าย __init__ คืน Informal String Representation ของออบเจ็กต์ เหมาะสำหรับ Debugging ตัวอย่าง class Time: def __str__(self): return %.2d:%.2d:%.2d % (self.hour, self.minute, self.second) เมื่อ print ออบเจ็กต์ Python จะเรียก __str__: >>>

  16. ฟังก์ชัน cmp() ใน Python OOP ทำอะไร

    ฟังก์ชัน cmp() cmp(x, y) เปรียบเทียบค่าของ 2 อาร์กิวเมนต์ x และ y คืนค่าติดลบ หาก x < y คืนค่า 0 หาก x == y คืนค่าบวก หาก x > y Built-in cmp() มักคืน -1, 0, 1 เท่านั้น แต่ฟังก์ชันอื่นที่มี signature เดียวกันอาจคืนค่าอื่น ควรดูแค่เครื่องหมายของผลลัพธ์ >>> cmp(2, 8) # -

  17. ความแตกต่างระหว่าง str() และ repr() ใน Python

    เอกสาร Python ระบุว่า __repr__ ใช้หา Official String Representation ของออบเจ็กต์ ส่วน __str__ ใช้หา Informal String Representation print และ str() ใช้ __str__ ส่วน repr() ใช้ __repr__ ตัวอย่างกับ datetime >>> import datetime >>> today = datetime.datetime.now() >>> str(t

  18. Base Overloading Methods ใน Python คืออะไร

    คลาส Python ให้คุณสมบัติมาตรฐานของ OOP ทั้งหมด: กลไกการสืบทอดคลาสรองรับ Multiple Base Classes คลาสลูก (Derived Class) สามารถ Override เมธอดของ Base Class ใดๆ ได้ และเมธอดสามารถเรียกเมธอดของ Base Class ที่มีชื่อเดียวกันได้

  19. Operator Overloading ใน Python ทำงานอย่างไร?

    เรารู้ว่าเราสามารถใช้ตัวดำเนินการ + สำหรับบวกตัวเลข และในเวลาเดียวกันใช้สำหรับ Concatenate Strings ได้ สิ่งนี้เป็นไปได้เพราะตัวดำเนินการ + ถูก Overload โดยทั้ง Class int และ Class str ตัวดำเนินการพื้นฐานคือ Methods ที่ถูกกำหนดใน Class เหล่านั้น การกำหนด Methods สำหรับ Operators เรียกว่า Operator Ove

  20. Data Hiding ใน Python OOP คืออะไร

    ตามเอกสาร Python Data Hiding เกี่ยวกับการแยก Client จาก (ส่วนหนึ่งของ) Implementation บางออบเจ็กต์ของโมดูลอาจเป็น Internal ของโมดูลและมองไม่เห็น/เข้าถึงไม่ได้จากผู้ใช้ วิธีนี้ใช้หลีกเลี่ยง Dependency และให้ความปลอดภัยพร้อมกัน ผู้ใช้แอปพลิเคชันรู้ว่าใช้งานอย่างไร แต่ไม่จำเป็นต้องรู้ว่าแอปพลิเคชันทำง

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:46/451  20-คอมพิวเตอร์/Page Goto:1 40 41 42 43 44 45 46 47 48 49 50 51 52