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

ฟังก์ชัน Python เป็น Objects หรือไม่

ใช่ ฟังก์ชันใน Python เป็น first-class objects**

Python สร้าง function objects เมื่อใช้ def หรือ lambda สามารถกำหนด attributes ได้เหมือนออบเจ็กต์ทั่วไป

ตัวอย่าง

def foo(): pass
foo.score = 20
print(type(foo))      # 
print(foo.score)      # 20
print(type(lambda x: x))  # 

ฟังก์ชันมี built-in attribute __doc__ (docstring) และสามารถมี nested functions ได้