Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python TypeError: 'method' object is not subscriptable - แก้ไขอย่างไร?

Arguments ใน Python methods ต้องระบุใน parentheses เพราะ functions และ methods ใช้ parentheses บอกว่าถูกเรียก ถ้าใช้ square brackets เรียก method จะเจอ error TypeError: 'method' object is not subscriptable

คู่มือนี้อธิบายว่า error นี้หมายถึงอะไร และทำไมถึงเจอ พร้อมตัวอย่างเพื่อช่วยพัฒนา solution

TypeError: 'method' object is not subscriptable

Subscriptable objects คือ objects ที่มี __getitem__ method เช่น lists, dictionaries, tuples __getitem__ method ช่วยให้ Python interpreter ดึงสมาชิกเดี่ยวจาก collection ได้

ไม่ใช่ทุก objects ที่ subscriptable Methods ตัวอย่าง ไม่ได้ implement __getitem__ method หมายความว่าไม่สามารถใช้ square bracket syntax เข้าถึง items ใน method หรือเรียก method ได้

ตัวอย่างโค้ด:

cheeses = ["Edam", "Stilton", "English Cheddar", "Parmesan"]
print(cheeses[0])

โค้ดคืนค่า "Edam" ชีสที่ index 0 ไม่สามารถใช้ square brackets เรียก function หรือ method ได้ เพราะ functions และ methods ไม่ใช่ subscriptable objects

ตัวอย่างสถานการณ์ (Example Scenario)

สร้างโปรแกรมเก็บชีสใน objects คลาส "Cheese" สำหรับกำหนดชีส จะมีเมธอดเช็คว่าชีสตัวนั้นมาจากประเทศไหน

เริ่มจากกำหนดคลาส Cheese:

class Cheese:
    def __init__(self, name, origin):
        self.name = name
        self.origin = origin
    def get_country(self, to_compare):
        if to_compare == self.origin:
            print("{} is from {}.".format(self.name, self.origin))
        else:
            print("{} is not from {}. It is from {}.".format(self.name, to_compare, self.origin))

คลาสมี 2 เมธอด: 1) กำหนดโครงสร้าง Cheese object 2) เช็คว่า country of origin ของชีสเท่ากับค่าที่กำหนดหรือไม่

สร้าง object จากคลาส Cheese:

edam = Cheese("Edam", "Netherlands")

ตัวแปร "edam" เป็น object ชื่อชีสคือ Edam มาจาก Netherlands

เรียกเมธอด get_country():

edam.get_country["Germany"]

โค้ดรัน get_country() จากคลาส Cheese เมธอดเช็คว่า "origin" ใน "edam" object เท่ากับ "Germany" ไหม

รันโค้ดดู:

Traceback (most recent call last):
File "main.py", line 14, in 
edam.get_country["Germany"]
TypeError: 'method' object is not subscriptable

Error เกิดในโค้ดของเรา

วิธีแก้ (The Solution)

วิเคราะห์บรรทัดที่ Python debugger ชี้ว่า error:

edam.get_country["Germany"]

บรรทัดนี้ใช้ square brackets เรียก get_country() method ไวยากรณ์นี้ไม่ถูกต้อง เพราะ square brackets ใช้เข้าถึง items จาก list เนื่องจาก functions และ objects ไม่ subscriptable จึงไม่สามารถใช้ square brackets เรียกได้

เพื่อแก้ error ต้องเปลี่ยน square brackets เป็น parentheses (curly brackets ในภาษาไทยมักเรียกว่า parentheses หรือ วงเล็บ):

edam.get_country("Germany")

รันโค้ดดู:

Edam is not from Germany. It is from Netherlands.

โค้ดรันสำเร็จ ลองเช็คว่า Edam มาจาก "Netherlands" ไหม เพื่อทดสอบทั้ง 2 กรณี:

edam.get_country("Netherlands")

ผลลัพธ์:

Edam is from Netherlands.

โค้ดทำงานได้ทั้ง 2 กรณี ไม่ว่าค่าที่ระบุจะเท่ากับประเทศของชีสหรือไม่

สรุป

Error TypeError: 'method' object is not subscriptable เกิดเมื่อใช้ square brackets เรียก method ภายในคลาส วิธีแก้: ให้แน่ใจว่าเรียก methods ของคลาสด้วย parentheses หลังชื่อเมธอดที่ต้องการเรียก

ตอนนี้คุณพร้อมแก้ Python error พบบ่อยนี้อย่างมืออาชีพแล้ว!