zoukankan      html  css  js  c++  java
  • Python使用inspect查看代码参数

    使用import inspect查看python 类的参数和模块、函数代码

     文件就是最小的模块,文件夹是比较大的模块。

    文件里面可以包含类,函数。

    函数可以执行一个操作,多个函数组合在一起可以写为一个模块,根据不同事物写成一个类,这个类包含几个行为写成几个类内的函数,也可以将这些作为一个文件。

    主要步骤是将文件路径设置到系统,再将文件作为模块引入,再开始查看文件里面的内容。

    首先,写了一个函数

    复制代码
    def h():
        print "hello"
    
    def hm(m,k):
        print m, k
    
    
    class w(object):
        def __init__(a, self):
            name =a
        def g(self):
            print name,"hello world!"
    复制代码

    保存在路径C:下,起个名字叫hello.py

    打开python shell 窗口,将这个路径加入到系统路径里。命令如下

    import sys

    sys.path.append('C:/')

    将文件当做一个模块引入。

    import hello

    import inspect

    查看整个模块hello的源代码: inspect.getsource(hello)  整个样子不好看,需要print inspect.getsource(hello)

    查看模块hello里面wo这个类的全部代码  print inspect.getsource(hello.w)

    查看模块内某个函数的代码: print inspect.getsource(hello.h)

    查看模块内某个类中函数的代码 print inspect.getsource(hello.w.g)

    查看模块中某个函数的参数的代码:inspect.getargspec(hello.hm)

    查看模块中类的参数代码 inspect.getargspec(hello.w.__init__)   #这里还是查看类的初始定义函数。

    查看类中函数参数代码 inspect.getargspec(hello.w.g)

    查看模块路径 inspect.getabsfile(hello)  

    查看文件夹模块中某个类的路径 inspect.getabsfile(。。。)#结果是显示类的初始定义函数__init__.py的位置。

     >>> inspect.getabsfile(django.db)

    'c:\python27\lib\site-packages\django-1.7.1-py2.7.egg\django\db\__init__.py'

    综上,最重要的是要记住,

    查看全部代码 inspect.getsource(模块.函数)或者(模块.类.函数)

    查看函数参数 inspect.getargspec(...)   查看类的参数,则括号里为(模块.类.__init__)

    查看函数的位置 inspect.getabsfile(...) 

    这些应该够学习用了。以后有什么再补充。

  • 相关阅读:
    oob中程序的监视
    使用内联 XAML
    silverlight 导航注意点
    动画入门,用actionscript实现A*寻路算法【游戏自动寻路】 转
    Remoting 转
    XSD文件 转
    Security 转
    游戏开发(程序)职位招聘的一些感受和经验 转
    WCF 转
    WPF 转
  • 原文地址:https://www.cnblogs.com/nemolmt/p/6701024.html
Copyright © 2011-2022 走看看