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

วิธีเปลี่ยนอาร์กิวเมนต์ของฟังก์ชันเป็นตัวพิมพ์เล็กใน Python

ใช้โมดูล inspect เพื่อดึงชื่อพารามิเตอร์ของฟังก์ชัน แล้วนำไปแปลงเป็นตัวพิมพ์เล็ก

ตัวอย่าง

import inspect

def foo(ARG1, ARG2):
    pass

arg_names = inspect.getargspec(foo)[0]
lower_args = [item.lower() for item in arg_names]
print(lower_args)

ผลลัพธ์

['arg1', 'arg2']

inspect.getargspec() คืนค่า ArgSpec object ซึ่ง index 0 คือ list ของชื่อพารามิเตอร์ปกติ