ผลต่างระหว่างรุ่นของ "Python Programming/Objects"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 164: แถว 164:
 
|-  
 
|-  
 
| replace(old, new[, count])
 
| replace(old, new[, count])
| แทนสตริง old ทุกตัวที่ปรากฎอยู่ใน method receiver ด้วย new ถ้ากำหนด count มาด้วยจะแทนเฉพาะ old เพียง count ตัวแรกเท่านั้น
+
| คืนสตริงที่เกิดจากการแทนสตริง old ทุกตัวที่ปรากฎอยู่ใน method receiver ด้วย new ถ้ากำหนด count มาด้วยจะแทนเฉพาะ old เพียง count ตัวแรกเท่านั้น
 
|-
 
|-
 
| split([sep [,maxsplit]])
 
| split([sep [,maxsplit]])
แถว 172: แถว 172:
 
| คืนสตริงที่ได้จากการตัด whitespace ออกจากหัวและท้ายของ method receiver
 
| คืนสตริงที่ได้จากการตัด whitespace ออกจากหัวและท้ายของ method receiver
 
|}
 
|}
 
+
<pre title="interpreter">
 +
>>> s = "  spam sausage spam spam bacom spam tomato and spam  "
 +
>>> s.strip()
 +
'spam sausage spam spam bacom spam tomato and spam'
 +
>>> s.count("spam")
 +
5
 +
>>> s.find("spam")
 +
3
 +
>>> s.replace("spam", "ham")
 +
'  ham sausage ham ham bacom ham tomato and ham  '
 +
>>> s.split()
 +
['spam', 'sausage', 'spam', 'spam', 'bacom', 'spam', 'tomato', 'and', 'spam']
 +
>>> '--'.join(s.split())
 +
'spam--sausage--spam--spam--bacom--spam--tomato--and--spam'
 +
>>> s.index("spam")
 +
3
 +
>>> s.index("ham")
 +
Traceback (most recent call last):
 +
  File "<stdin>", line 1, in <module>
 +
ValueError: substring not found
 +
</pre>
 
{{Python Programming/Navigation|Dictionaries|Classes}}
 
{{Python Programming/Navigation|Dictionaries|Classes}}

รุ่นแก้ไขเมื่อ 18:12, 19 ตุลาคม 2551

ไพทอนสนับสนุนการเขียนโปรแกรมเชิงวัตถุ (OOP) ในระัดับหนึ่ง object ค่าทุกค่าในไพทอนล้วนเป็น object ทั้งสิ้น นี่รวมไปถึงค่าที่เป็น primitive type อย่างค่าประเภท int, float, string, และ boolean นอกจากนี้ list, tuple, และ dictionary ก็เป็น object เช่นกัน

การเรียก method ของ object ในภาษาไพทอนจะเหมือนกับการเรียก method ในภาษา Java โดยมีรูปแบบดังนี้

<<object>>.<<ชื่อ method>>(<<parameter 1>>, <<parameter 2>>, ...)

ส่วนที่เหลือของหน้านี้จะกล่าวถือ method ของ object ต่างๆ ที่เราเคยพูดถึงมาแล้ว

Method ของ List

ส่วนนี้ลอกมาจาก Python Tutorial: 5. Data Structures

ชื่อ หน้าที่
append(x)

เพิ่ม x ต่อท่าย list

insert(i, x)

แทรก x ที่ำตำแหน่ง i ใน list

remove(x)

ลบ x ออกจาก list

pop([i])

ลบสมาชิกที่ตำแหน่งที่ i ออกจา่ก list และคืนมันกลับให้ผู้เรียก method ถ้าไม่ให้ค่า i มันจะลบสมาชิกตัวสุดท้ายใน list

index(x)

คืนตำแหน่งแรกใน list ที่ค่าของ list ที่ำตำแหน่งนั้นเท่ากับ x

count(x)

นับจำนวน x ที่ปรากฎใน list

sort()

เรียงลำดับค่าใน list

reverse()

กลับลำดับของค่าใน list จากหน้าไปหลังเป็นหลังไปหน้า

ตัวอย่าง

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print a.count(333), a.count(66.25), a.count('x')
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]

Method ของ Dictionary

ชื่อ หน้าที่
append(x)

เพิ่ม x ต่อท่าย list

has_key(k) ตรวจสอบว่าใน dictionary มี key k หรือไม่
items() คืน list ของคู่ลำดับ (key, value) ทั้งหมดใน dictionary ออกมา
keys() คืน list ของค่า key ทั้งหมดใน dictionary ออกมา
values() คืน list ของค่า value ทั้งหมดใน dictionary ออกมา
clear() ลบการจับคู่ทั้งหมดออกจาก dictionary
>>> tel = {'guido':4127, 'jack':4098, 'sape':4139}
>>> tel.keys()
['sape', 'jack', 'guido']
>>> tel.values()
[4139, 4098, 4127]
>>> tel.items()
[('sape', 4139), ('jack', 4098), ('guido', 4127)]
>>> tel.has_key('guido')
True
>>> tel.clear()
>>> tel
{}

Method ของสตริง

ชื่อ หน้าที่
count(sub[, start[, end]]) นับจำนวนสตรีิง sub ใน method receiver ถ้าระบุ start และ end มาด้วยจะนับเฉพาะระหว่างตัวอักษรตำแหน่งที่ start ถึงตำแหน่งที่ end-1
find(sub[, start[, end]]) คืนตำแหน่งที่สตริง sub ปรากฎอยู่ใน method receiver ถ้า sub ปรากฎอยู่หลายที่จะคืนตำแหน่งที่น้อยที่สุด ถ้าระบุ start และ end มาด้วยจะค้นหาเฉพาะระหว่างตัวอักษรตำแหน่งที่ start ถึงตำแหน่งที่ end-1
index(sub[, start[, end]]) เหมือน find แต่จะเกิด error ขึ้นถ้าไม่พบ sub ใน method receiver
join(seq) ถ้า seq เป็น list หรือ tuple เมธอดนี้จะคืนสตริงที่เกิดจากการนำสตริงใน seq มาต่อกันโดยใช้ method receiver เป็นตัวคั่นระหว่างสตริงสองตัวใดๆ
replace(old, new[, count]) คืนสตริงที่เกิดจากการแทนสตริง old ทุกตัวที่ปรากฎอยู่ใน method receiver ด้วย new ถ้ากำหนด count มาด้วยจะแทนเฉพาะ old เพียง count ตัวแรกเท่านั้น
split([sep [,maxsplit]]) คืน list ของสตริงที่เกิดจากการแบ่ง receiver ออกเป็นส่วนๆ โดยมี sep เป็นตัวคั่นระหว่างส่วน ถ้าไม่ให้ระบุ sep มาให้จะใช้ช่องว่างหรือตัวอักษรประเภท whitespace อื่นๆ เป็นตัวขั้น ถ้าระุบุ maxsplit มาด้วยจะมีการแบ่งเพียง maxsplit ครั้งเท่านั้น
strip([chars]) คืนสตริงที่ได้จากการตัด whitespace ออกจากหัวและท้ายของ method receiver
>>> s = "   spam sausage spam spam bacom spam tomato and spam   "
>>> s.strip()
'spam sausage spam spam bacom spam tomato and spam'
>>> s.count("spam")
5
>>> s.find("spam")
3
>>> s.replace("spam", "ham")
'   ham sausage ham ham bacom ham tomato and ham   '
>>> s.split()
['spam', 'sausage', 'spam', 'spam', 'bacom', 'spam', 'tomato', 'and', 'spam']
>>> '--'.join(s.split())
'spam--sausage--spam--spam--bacom--spam--tomato--and--spam'
>>> s.index("spam")
3
>>> s.index("ham")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
หน้าก่อน: Dictionaries สารบัญ หน้าต่อไป: Classes