Slice Operator สำหรับ Python Sequence Data Types ใช้ดึง Element แรก 5 ตัวจาก List ได้ง่ายๆ ลิงก์ด้านล่างเป็น Tutorial เกี่ยวกับการดึง N Items แรกจาก List ใน Python มีตัวอย่างง่ายๆ เว็บนี้มีประโยชน์มากสำหรับการเรียนเขียนโค้ด
Slice Operator สำหรับ Python Sequence Data Types ใช้ดึง Element แรก 5 ตัวจาก List ได้ง่ายๆ ลิงก์ด้านล่างเป็น Tutorial เกี่ยวกับการดึง N Items แรกจาก List ใน Python มีตัวอย่างง่ายๆ เว็บนี้มีประโยชน์มากสำหรับการเรียนเขียนโค้ด
แนวคิด OOP ใน Python Python เป็นภาษา Object-Oriented Programming สร้างและใช้ Classes และ Objects ได้ง่าย แนวคิดหลักของ OOP ใน Python: Object (วัตถุ) Class (คลาส) Method (เมธอด) Inheritance (การสืบทอด) Polymorphism (พอลิมอร์ฟิซึม) Data Abstraction (การสร้างนามธรรมข้อมูล)
ใน Python เกือบทุกโค้ดถูก Implement ด้วย Classes โปรแกรมเมอร์ใช้ Classes เก็บโค้ดที่เกี่ยวข้องร่วมกัน ใช้ Keyword class ซึ่งเป็น Collection ของ Object-oriented Constructs Class เป็น Template สำหรับสร้าง Objects Objects มี Member Variables และ Behavior ผูกมาด้วย ใน Python สร้าง Class ด้วย Keyword cl
การ Instantiate (Calling a Class Object) สร้าง Empty Object หลายคลาสต้องการสร้าง Object ด้วย Initial State เฉพาะ ดังนั้น Class อาจกำหนด Special Method ชื่อ __init__() ดังนี้: def __init__(self): self.data = [] เมื่อ Class กำหนด __init__() การ Instantiate จะ Invoke Method นี้อัตโ
self self ใช้แทน Instance ของคลาส ใช้ Keyword self เข้าถึง Attributes และ Methods ของคลาสใน Python __init__ Method __init__ เป็น Reserved Method ในคลาส Python เรียกว่า Constructor ใน OOP เรียกเมื่อสร้าง Object จากคลาส ให้คลาส Initialize Attributes ได้ ตัวอย่าง: คำนวณค่าใช้จ่ายสนามรูปสี่เหลี่ยม cla
Attributes ของคลาส เกือบทุกอย่างใน Python เป็น Object ทุก Object มี Attributes และ Methods Attributes จึงเป็นพื้นฐานสำคัญ Class เป็น Construct ที่รวบรวม Objects ที่คล้ายกัน Class ก็มี Attributes เช่นกัน มีความแตกต่างระหว่าง Class Attributes และ Instance Attributes: Class Attributes แชร์ระหว่าง Insta
ใน Python (และภาษาอื่นๆ) มีค่าที่หมายถึง ไม่มีค่า คือ None class Student: StudentName = None RollNumber = None เหล่านี้คล้าย Instance Variables ไม่ใช่ Class Variables ดังนั้นเขียนใน __init__ ดีกว่า: class Student(object): def __init__(self): self.StudentName = None sel
Class Variables คือ Variables ที่ประกาศ นอก เมธอด __init__ เป็น Static Elements หมายถึง 属于 Class ไม่ใช่ Instance แชร์กันทั้งหมดของ Class นั้น ตัวอย่าง class MyClass: __item1 = 123 __item2 = abc def __init__(self): pass เข้าใจชัดขึ้นกับโค้ดเพิ่มเติม: class MyClass: stat_elem
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
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
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:
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) ผลลัพ
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 =,
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
ตามเอกสาร Python Built-in Class Attributes มีดังนี้ (เข้าถึงด้วย Dot Operator เช่นเดียวกับ Attribute อื่น): classname.__dict__: Dictionary เก็บ Namespace ของคลาส classname.__doc__: Class Documentation String หรือ None หากไม่ได้กำหนด classname.__name__: ชื่อคลาส classname.__module__
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
ทุกฟังก์ชันมี 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
Built-in Attribute นี้พิมพ์ชื่อของ Class, Type, Function, Method, Descriptor หรือ Generator Instance ตัวอย่าง: ถ้า Python Interpreter รันโมดูลนั้นเป็น Main Program จะตั้งค่า __name__ เป็น __main__ หากถูก Import จากโมดูลอื่น __name__ จะเป็นชื่อโมดูลนั้น ตัวอย่าง class Bar(object): def foo():
Built-in Class Attribute นี้พิมพ์ชื่อโมดูลที่ Function/Object ถูกนิยามอยู่ หรือ None หากไม่มี ตัวอย่าง class A(object): pass class B(A): pass b = B() print(B.__module__) ผลลัพธ์ __main__
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