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

ฟังก์ชัน dir() ใน Python

ฟังก์ชัน built-in dir() คืนค่ารายการที่เรียงลำดับของสตริงที่มีชื่อที่ถูกกำหนดโดยโมดูล

รายการนี้มีชื่อของโมดูล ตัวแปร และฟังก์ชันทั้งหมดที่ถูกกำหนดในโมดูล ตัวอย่างง่ายๆ ดังนี้:

ตัวอย่าง

#!/usr/bin/python
# Import built-in module math
import math
content = dir(math)
print content

ผลลัพธ์ (Output)

เมื่อรันโค้ดข้างต้น จะได้ผลลัพธ์ดังนี้:

['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']

ที่นี่ ตัวแปรสตริงพิเศษ __name__ คือชื่อของโมดูล และ __file__ คือชื่อไฟล์ที่โมดูลถูกโหลดจาก