First-class citizens คือเอนทิตี้ที่เปิดใช้งานการสนับสนุนสำหรับทุกการดำเนินการที่อำนวยความสะดวกให้กับเอนทิตี้เพื่อนร่วมงาน
เอนทิตี้เหล่านี้มักใช้: ขณะส่งอาร์กิวเมนต์, คืนค่าจากฟังก์ชัน, การแก้ไขเงื่อนไข และการกำหนดค่า
ในบทความนี้ เราจะดูการนำไปใช้งานและการใช้งานของ first-class citizens ใน Python 3.x หรือเวอร์ชันก่อนหน้า นอกจากนี้ เราจะเรียนรู้ว่ามีเอนทิตี้อะไรบ้างที่ได้แท็กว่าเป็น First Class citizens
ผู้เมืองชั้นนำเหล่านี้รวมถึง Variables พร้อมกับ functions
มาทำความรู้จักชนิดข้อมูลที่เป็นส่วนหนึ่งของ First-class citizens
- Integers
- Floating type
- Complex Numbers
- Strings
ทั้งสี่ประเภทที่กล่าวมาข้างต้นได้รับแท็กว่าเป็น first-class citizens ใน Python 3.x หรือเวอร์ชันก่อนหน้า
ตัวอย่าง
# Declaration of an integer
print("hello world")
int_inp = int(input())
print("This is a First class Citizen of " + str(type(int_inp)))
# Declaration of floating type
float_inp = float(input())
print("This is a First class Citizen of " + str(type(float_inp)))
# Declaration of complex numbers
complex_inp = complex(input())
print("This is a First class Citizen of " + str(type(complex_inp)))
# Declaration of Strings
str_inp = input()
print("This is a First class Citizen of " + str(type(str_inp)))
Input
2 23.4 4+7j tutorialspoint
ผลลัพธ์
This is a First class Citizen of <class 'int'> This is a First class Citizen of <class 'float'> This is a First class Citizen of <class 'complex'> This is a First class Citizen of <class 'str'>
ตอนนี้มาดูฟังก์ชันบางอย่างที่ถูกอ้างถึงว่าเป็น first class citizens
First class objects ถูกจัดการอย่างสม่ำเสมอในภาษา Python เนื่องจากเป็น object-oriented ทุกเอนทิตี้อ้างอิงถึง default object ซึ่งสามารถอ้างอิงและถอดอ้างอิง (dereference) ได้ในทุกจุดของเวลา การจัดเก็บอาจทำได้โดยใช้ data structures หรือ control structures
ตอนนี้เราจะดูว่า Python รองรับ first-class functions หรือไม่ ดังนั้นภาษาใดก็ตามที่ถูกกล่าวว่ารองรับ first-class functions เมื่อมันจัดการฟังก์ชันเป็น first-class objects
ตัวอย่าง
# Python program
# functions being treated as objects
def comp_name(text):
return text.isupper()
print(comp_name("TUTORIALSPOINT"))
new_name = comp_name # referencing a function with the object
print(new_name("TutorialsPoint"))
ผลลัพธ์
True False
ตัวอย่าง
# Python program
# functions being passed as arguments to other functions
def new_inp(text):
return text.upper()
def old_inp(text):
return text.lower()
def display(func):
# storing the function in a normal variable
code = func("Love Coding, Learn everything on Tutorials Point")
print(code)
display(new_inp) # directly referenced by passing functions as arguments.
display(old_inp)
ผลลัพธ์
LOVE CODING, LEARN EVERYTHING ON TUTORIALS POINT love coding, learn everything on tutorials point
ที่นี่มองเห็นได้ชัดเจนว่า Python functions สามารถอ้างอิงได้โดยใช้ object และสามารถส่งเป็นอาร์กิวเมนต์ให้ฟังก์ชันอื่นได้ ซึ่งแสดงให้เห็นชัดเจนว่าใน Python functions เป็น First Class Citizens และสามารถอ้างอิงและถอดอ้างอิงได้โดยใช้ object entity
สรุป
ในบทความนี้ เราได้เรียนรู้การนำ max และ min function ที่รวมอยู่ใน Python Standard Library ไปใช้งาน