Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธี取得 N Items แรกจาก Generator หรือ List ใน Python

    Slice Operator สำหรับ Python Sequence Data Types ใช้ดึง Element แรก 5 ตัวจาก List ได้ง่ายๆ ลิงก์ด้านล่างเป็น Tutorial เกี่ยวกับการดึง N Items แรกจาก List ใน Python มีตัวอย่างง่ายๆ เว็บนี้มีประโยชน์มากสำหรับการเรียนเขียนโค้ด

  2. อธิบายแนวคิด OOP ใน Python ให้เข้าใจง่าย

    แนวคิด OOP ใน Python Python เป็นภาษา Object-Oriented Programming สร้างและใช้ Classes และ Objects ได้ง่าย แนวคิดหลักของ OOP ใน Python: Object (วัตถุ) Class (คลาส) Method (เมธอด) Inheritance (การสืบทอด) Polymorphism (พอลิมอร์ฟิซึม) Data Abstraction (การสร้างนามธรรมข้อมูล)

  3. วิธีนิยามคลาสใน Python

    ใน Python เกือบทุกโค้ดถูก Implement ด้วย Classes โปรแกรมเมอร์ใช้ Classes เก็บโค้ดที่เกี่ยวข้องร่วมกัน ใช้ Keyword class ซึ่งเป็น Collection ของ Object-oriented Constructs Class เป็น Template สำหรับสร้าง Objects Objects มี Member Variables และ Behavior ผูกมาด้วย ใน Python สร้าง Class ด้วย Keyword cl

  4. วิธีสร้าง Instance Objects ด้วย __init__ ใน Python

    การ Instantiate (Calling a Class Object) สร้าง Empty Object หลายคลาสต้องการสร้าง Object ด้วย Initial State เฉพาะ ดังนั้น Class อาจกำหนด Special Method ชื่อ __init__() ดังนี้: def __init__(self): self.data = [] เมื่อ Class กำหนด __init__() การ Instantiate จะ Invoke Method นี้อัตโ

  5. ความแตกต่างระหว่าง self และ __init__ ใน Python Class

    self self ใช้แทน Instance ของคลาส ใช้ Keyword self เข้าถึง Attributes และ Methods ของคลาสใน Python __init__ Method __init__ เป็น Reserved Method ในคลาส Python เรียกว่า Constructor ใน OOP เรียกเมื่อสร้าง Object จากคลาส ให้คลาส Initialize Attributes ได้ ตัวอย่าง: คำนวณค่าใช้จ่ายสนามรูปสี่เหลี่ยม cla

  6. วิธีนิยาม Attributes ของคลาสใน Python

    Attributes ของคลาส เกือบทุกอย่างใน Python เป็น Object ทุก Object มี Attributes และ Methods Attributes จึงเป็นพื้นฐานสำคัญ Class เป็น Construct ที่รวบรวม Objects ที่คล้ายกัน Class ก็มี Attributes เช่นกัน มีความแตกต่างระหว่าง Class Attributes และ Instance Attributes: Class Attributes แชร์ระหว่าง Insta

  7. วิธีประกาศ Attribute ใน Python โดยไม่มีค่า

    ใน Python (และภาษาอื่นๆ) มีค่าที่หมายถึง ไม่มีค่า คือ None class Student: StudentName = None RollNumber = None เหล่านี้คล้าย Instance Variables ไม่ใช่ Class Variables ดังนั้นเขียนใน __init__ ดีกว่า: class Student(object): def __init__(self): self.StudentName = None sel

  8. วิธีที่ถูกต้องในการนิยาม Class Variables ใน Python

    Class Variables คือ Variables ที่ประกาศ นอก เมธอด __init__ เป็น Static Elements หมายถึง 属于 Class ไม่ใช่ Instance แชร์กันทั้งหมดของ Class นั้น ตัวอย่าง class MyClass: __item1 = 123 __item2 = abc def __init__(self): pass เข้าใจชัดขึ้นกับโค้ดเพิ่มเติม: class MyClass: stat_elem

  9. อธิบายตัวแปรภายในและภายนอก __init__() ในคลาส Python

    Class Variables vs Instance Variables ตัวแปรทั้งหมด นอก ฟังก์ชัน __init__ ใน Python คือ Class Variables ส่วนที่อยู่ ใน __init__ คือ Instance Variables ความแตกต่างเข้าใจง่ายจากโค้ด: ตัวอย่าง class MyClass: stat_elem = 456 # Class Variable def __init__(self): self.object_ele

  10. ฟังก์ชัน getattr() ใน Python ทำอะไร

    Python getattr() คืนค่าของ Named Attribute ของ Object หาไม่เจอ คืน Default Value ที่ให้มา Syntax getattr(object, name[, default]) Parameters: Object, Attribute Name, Default (Optional) Returns: Attribute Value, หรือ Default, หรือ Raise AttributeError หากไม่มี Default ตัวอย่าง class Male: age

  11. ฟังก์ชัน hasattr() ใน Python ทำอะไร

    hasattr() Method คืน True หาก Object มี Named Attribute ที่กำหนด False หากไม่มี Syntax hasattr(object, name) hasattr() ถูกเรียกโดย getattr() เพื่อเช็คว่าจะ Raise AttributeError หรือไม่ Parameters: Object, Attribute Name Returns: True (มี Attribute), False (ไม่มี Attribute) ตัวอย่าง class Male:

  12. ฟังก์ชัน setattr() ใน Python ทำอะไร

    setattr() Method ตั้งค่าของ Named Attribute ของ Object Syntax setattr(object, name, value) Parameters: Object, Attribute Name, Value Returns: None ตัวอย่าง class Male: name = Abel x = Male() print(Before modification:, x.name) setattr(x, name, Jason) print(After modification:, x.name) ผลลัพ

  13. ฟังก์ชัน delattr() ใน Python ทำอะไร

    Python delattr() delattr() ลบ Attribute จาก Object หาก Object อนุญาต Syntax delattr(object, name) Parameters: Object, Attribute Name Returns: None (แค่ลบ Attribute หาก Object อนุญาต) ตัวอย่าง class Coordinate: x = 12 y = -7 z = 0 point1 = Coordinate() print(x =, point1.x) print(y =,

  14. ความแตกต่างระหว่าง getattr() และ setattr() ใน Python

    getattr() Method คืนค่าของ Named Attribute ของ Object หาไม่เจอ คืน Default Value ที่ให้มา Syntax getattr(object, name[, default]) Parameters: Object, Attribute Name, Default (Optional) Returns: Attribute Value, หรือ Default, หรือ Raise AttributeError หากไม่มี Default setattr() Method ตั้งค่าของ N

  15. Built-in Class Attributes ใน Python มีอะไรบ้าง

    ตามเอกสาร Python Built-in Class Attributes มีดังนี้ (เข้าถึงด้วย Dot Operator เช่นเดียวกับ Attribute อื่น): classname.__dict__: Dictionary เก็บ Namespace ของคลาส classname.__doc__: Class Documentation String หรือ None หากไม่ได้กำหนด classname.__name__: ชื่อคลาส classname.__module__

  16. Built-in Class Attribute __dict__ ใน Python ทำอะไร

    Special Attribute ของทุกโมดูลคือ __dict__ เป็น Dictionary ที่เก็บ Symbol Table ของโมดูล object.__dict__ Dictionary หรือ Mapping Object ใช้เก็บ Attributes (Writable) ของ Object ตัวอย่าง class MyClass(object): class_var = 1 def __init__(self, i_var): self.i_var = i_var foo = MyClas

  17. Built-in Class Attribute __doc__ ใน Python ทำอะไร

    ทุกฟังก์ชันมี Built-in Attribute __doc__ คืน Docstring ที่นิยามใน Source Code ของฟังก์ชัน def foo(): This is an example of how a doc_string looks like. This string gives useful information about the function being defined. pass print(foo.__doc__) ผลลัพธ์: This is an example of h

  18. Built-in Class Attribute __name__ ใน Python ทำอะไร

    Built-in Attribute นี้พิมพ์ชื่อของ Class, Type, Function, Method, Descriptor หรือ Generator Instance ตัวอย่าง: ถ้า Python Interpreter รันโมดูลนั้นเป็น Main Program จะตั้งค่า __name__ เป็น __main__ หากถูก Import จากโมดูลอื่น __name__ จะเป็นชื่อโมดูลนั้น ตัวอย่าง class Bar(object): def foo():

  19. Built-in Class Attribute __module__ ใน Python ทำอะไร

    Built-in Class Attribute นี้พิมพ์ชื่อโมดูลที่ Function/Object ถูกนิยามอยู่ หรือ None หากไม่มี ตัวอย่าง class A(object): pass class B(A): pass b = B() print(B.__module__) ผลลัพธ์ __main__

  20. Built-in Class Attribute __bases__ ใน Python ทำอะไร

    Built-in Class Attribute นี้พิมพ์ Tuple ของ Base Classes ของ Class Object ตัวอย่าง class A(object): pass class B(A): pass b = B() print(B.__bases__) ผลลัพธ์ (<class __main__.A>,) คืน Tuple ของ Parent Classes ตามลำดับใน Class Definition

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