Python的反射機制可以動態獲取物件資訊以及動態呼叫物件,本文介紹如何獲取物件中的函式注釋資訊以及引數資訊,
定義一個Person類:
class Person():
def talk(self, name, age, height=None):
"""talk function
:return:
"""
print(f"My name is {name}")
print(f"My age is {age}")
if height is not None:
print(f"My height is {height}")
dir() 命令也可以獲取函式的屬性資訊:
person = Person()
print(dir(person))
func = getattr(person, "talk")
print(dir(func))
結果
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'talk']
['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__func__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
獲取函式注釋資訊
可以通過 doc 屬性來獲取注釋資訊(三引號括起來的注釋):
func = getattr(person, "talk")
print(func.__doc__)
結果
talk function
:return:
獲取函式引數
1、 通過 __code__ 屬性讀取函式引數資訊
>> print(dir(func.__code__))
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_kwonlyargcount', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames']>>
#學習中遇到問題沒人解答?小編創建了一個Python學習交流群:725638078
print("co_name: ", func.__code__.co_name) # 回傳函式名
print("co_argcount: ", func.__code__.co_argcount) # 回傳函式的引數個數
print("co_varnames: ",func.__code__.co_varnames) # 回傳函式的引數
print("co_filename: ", func.__code__.co_filename) # 回傳檔案絕對路徑
print("co_consts: ", func.__code__.co_consts)
print("co_firstlineno: ",func.__code__.co_firstlineno) # 回傳函式行號
print("co_kwonlyargcount: ",func.__code__.co_kwonlyargcount) # 關鍵字引數
print("co_nlocals: ",func.__code__.co_nlocals) # 回傳區域變數個數
結果
co_name: talk
co_argcount: 4
co_varnames: ('self', 'name', 'age', 'height')
co_filename: D:/ProgramWorkspace/PythonNotes/00-Python-Essentials/demo.py
co_consts: ('talk function\n :return:\n ', 'My name is ', 'My age is ', None, 'My height is ')
co_firstlineno: 44
co_kwonlyargcount: 0
co_nlocals: 4
通過 code.co_varnames 可以獲取引數名,引數默認值可以通過如下方式獲得:
print(func.__defaults__)
結果
(None,)
2、通過inspect庫來讀取函式引數資訊
除了用__code__ 屬性外還可以使用inspect庫來讀取函式引數,使用getfullargspec和signature方法來讀取函式引數:
import inspect
# inspect.getargspec(func) # python2
argspec = inspect.getfullargspec(func)
print(argspec.args)
print(argspec.defaults)
print(argspec.varkw)
sig = inspect.signature(func)
print(sig)
結果
['self', 'name', 'age', 'height']
(None,)
None
(name, age, height=None)
也可以在函式內部使用:
class Person():
def talk(self, name, age, height=None):
"""talk function
:return:
"""
frame = inspect.currentframe()
args, _, _, values = inspect.getargvalues(frame)
print(inspect.getframeinfo(frame))
print(f'function name: {inspect.getframeinfo(frame).function}')
for i in args:
print(f"{i} = {values[i]}")
if __name__ == '__main__':
p = Person()
p.talk("zhangsan", 18, height=175)
結果
Traceback(filename='D:/ProgramWorkspace/PythonNotes/00-Python-Essentials/demo.py', lineno=44, function='talk', code_context=[' print(inspect.getframeinfo(frame))\n'], index=0)
function name: talk
self = <__main__.Person object at 0x0000023E4CF17B08>
name = zhangsan
age = 18
height = 175
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/549390.html
標籤:其他
上一篇:Spring注解實作回圈重試功能
下一篇:數字營銷(一)客戶畫像淺談
