Các hàm trong module này cung cấp thông tin hữu ích về đối tượng trực tiếp như module, class, method, function, code object, v.v. Các hàm này thực hiện kiểm tra kiểu, truy xuất mã nguồn, kiểm tra class và function, và kiểm tra stack trình thông dịch.
getmembers() – Hàm này trả về tất cả thành viên của một đối tượng trong danh sách cặp (tên, giá trị) sắp xếp theo tên. Nếu predicate tùy chọn được cung cấp, chỉ bao gồm thành viên mà predicate trả về True.
getmodulename() – Hàm này trả về tên module được đặt tên bởi đường dẫn file, không bao gồm tên các gói bao quanh.
Chúng ta sẽ dùng script sau để hiểu hành vi của module inspect.
# inspect-example.py
'''This is module docstring'''
def hello():
'''hello docstring'''
print('Hello world')
return
# Định nghĩa class
class parent:
'''parent docstring'''
def __init__(self):
self.var = 'hello'
def hello(self):
print(self.var)
class child(parent):
def hello(self):
'''hello function overridden'''
super().hello()
print("How are you?")
bắt đầu bằng '__'
>>> import inspect, inspect_example
>>> for k, v in inspect.getmembers(inspect_example):
if k.startswith('__') == False:
print(k, v)
child hello parent
>>>
Predicate (Vị từ)
Predicate là điều kiện logic áp dụng cho các hàm trong module inspect. Ví dụ getmembers() trả về danh sách thành viên của module mà predicate cho trước trả về True. Các predicate sau được định nghĩa trong inspect:
| ismodule() | Trả về True nếu đối tượng là module. |
| isclass() | Trả về True nếu đối tượng là class, whether built-in hoặc tạo trong code Python. |
| ismethod() | Trả về True nếu đối tượng là bound method viết bằng Python. |
| isfunction() | Trả về True nếu đối tượng là function Python, bao gồm function tạo bởi lambda expression. |
| isgenerator() | Trả về True nếu đối tượng là generator. |
| iscode() | Trả về True nếu đối tượng là code. |
| isbuiltin() | Trả về True nếu đối tượng là built-in function hoặc bound built-in method. |
| isabstract() | Trả về True nếu đối tượng là abstract base class. |
Ở đây chỉ thành viên class trong module được trả về.
>>> for k, v in inspect.getmembers(inspect_example, inspect.isclass):
print(k, v)
child
parent
>>>
Để truy xuất thành viên của class 'child' chỉ định:
>>> inspect.getmembers(inspect_example.child) >>> x = inspect_example.child() >>> inspect.getmembers(x)
Hàm getdoc() truy xuất docstring của module, class hoặc function.
>>> inspect.getdoc(inspect_example) 'This is module docstring' >>> inspect.getdoc(inspect_example.parent) 'parent docstring' >>> inspect.getdoc(inspect_example.hello) 'hello docstring'
Hàm getsource() lấy mã định nghĩa của function:
>>> print(inspect.getsource(inspect_example.hello))
def hello():
'''hello docstring'''
print('Hello world')
return
>>> sign = inspect.signature(inspect_example.parent.hello)
>>> print(sign)
Module inspect cũng có giao diện dòng lệnh.
C:\Users\acer>python -m inspect -d inspect_example Target: inspect_example Origin: C:\python36\inspect_example.py Cached: C:\python36\__pycache__\inspect_example.cpython-36.pyc Loader: <_frozen_importlib_external.SourceFileLoader object at 0x0000029827BD0D30>
Lệnh sau trả về mã nguồn của function 'Hello()' trong module.
C:\Users\acer>python -m inspect inspect_example:hello
def hello():
'''hello docstring'''
print('Hello world')
return