python可以用help查看内置定义的函数;对于用户自定义的函数怎么看呢?如下:
例如,定义了一个函数func1:
>>> def func1(arg1,arg2):
... return arg1+arg2
通过func1.__code__(注意,“code"前后都是连续两个英文字符下划线“_”)可以查看func1的参数及属性:
>>> print(dir(func1.__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']
如,查看函数的参数:
>>> print(func1.__code__.co_varnames)
('arg1', 'arg2')