utils类
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
def test1(age, name):
other = age
print(other)
print(age, name)
def test2(age, name, grade):
print(age, name, grade)
获取utils类中的方法
import utils
# 获取所有方法
print(dir(utils))
# 过滤掉内置方法
print([item for item in dir(utils) if not (item.startswith("__") and item.endswith("__"))])
[‘builtins’, ‘cached’, ‘doc’, ‘file’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘test1’, ‘test2’]
[‘test1’, ‘test2’]
获取utils.test1中的参数个数
import utils
print(utils.test1.__code__.co_argcount)
print(getattr(utils, 'test1').__code__.co_argcount)
2
2
获取utils.test1中的所有使用的参数
import utils
print(utils.test1.__code__.co_varnames)
print(getattr(utils, 'test1').__code__.co_varnames)
(‘age’, ‘name’, ‘other’)
(‘age’, ‘name’, ‘other’)
获取utils.test1函数中定义的参数
import utils
count = utils.test1.__code__.co_argcount
print(utils.test1.__code__.co_varnames[0: count])
count2 = getattr(utils, 'test1').__code__.co_argcount
print(getattr(utils, 'test1').__code__.co_varnames[0:count])
(‘age’, ‘name’)
(‘age’, ‘name’)