python查看函数源代码

除了在系统中找到源文件直接打开查看,还可以用inspect.getsource函数查看

示例1

import inspectdef print_hh():print('hh')source = inspect.getsource(print_hh) # 查看自定义函数print_hh的源代码
print(source)

运行结果:

def print_hh():print('hh')

示例2

import inspect
import numpy as npsource = inspect.getsource(np.sum) # 查看系统函数numpy加和函数sum的源代码
print(source)

运行结果:

@array_function_dispatch(_sum_dispatcher)
def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue,initial=np._NoValue, where=np._NoValue):"""Sum of array elements over a given axis.Parameters----------a : array_likeElements to sum.axis : None or int or tuple of ints, optionalAxis or axes along which a sum is performed.  The default,axis=None, will sum all of the elements of the input array.  Ifaxis is negative it counts from the last to the first axis... versionadded:: 1.7.0If axis is a tuple of ints, a sum is performed on all of the axesspecified in the tuple instead of a single axis or all the axes asbefore.dtype : dtype, optionalThe type of the returned array and of the accumulator in which theelements are summed.  The dtype of `a` is used by default unless `a`has an integer dtype of less precision than the default platforminteger.  In that case, if `a` is signed then the platform integeris used while if `a` is unsigned then an unsigned integer of thesame precision as the platform integer is used.out : ndarray, optionalAlternative output array in which to place the result. It must havethe same shape as the expected output, but the type of the outputvalues will be cast if necessary.keepdims : bool, optionalIf this is set to True, the axes which are reduced are leftin the result as dimensions with size one. With this option,the result will broadcast correctly against the input array.If the default value is passed, then `keepdims` will not bepassed through to the `sum` method of sub-classes of`ndarray`, however any non-default value will be.  If thesub-class' method does not implement `keepdims` anyexceptions will be raised.initial : scalar, optionalStarting value for the sum. See `~numpy.ufunc.reduce` for details... versionadded:: 1.15.0where : array_like of bool, optionalElements to include in the sum. See `~numpy.ufunc.reduce` for details... versionadded:: 1.17.0Returns-------sum_along_axis : ndarrayAn array with the same shape as `a`, with the specifiedaxis removed.   If `a` is a 0-d array, or if `axis` is None, a scalaris returned.  If an output array is specified, a reference to`out` is returned.See Also--------ndarray.sum : Equivalent method.add.reduce : Equivalent functionality of `add`.cumsum : Cumulative sum of array elements.trapz : Integration of array values using the composite trapezoidal rule.mean, averageNotes-----Arithmetic is modular when using integer types, and no error israised on overflow.The sum of an empty array is the neutral element 0:>>> np.sum([])0.0For floating point numbers the numerical precision of sum (and``np.add.reduce``) is in general limited by directly adding each numberindividually to the result causing rounding errors in every step.However, often numpy will use a  numerically better approach (partialpairwise summation) leading to improved precision in many use-cases.This improved precision is always provided when no ``axis`` is given.When ``axis`` is given, it will depend on which axis is summed.Technically, to provide the best speed possible, the improved precisionis only used when the summation is along the fast axis in memory.Note that the exact precision may vary depending on other parameters.In contrast to NumPy, Python's ``math.fsum`` function uses a slower butmore precise approach to summation.Especially when summing a large number of lower precision floating pointnumbers, such as ``float32``, numerical errors can become significant.In such cases it can be advisable to use `dtype="float64"` to use a higherprecision for the output.Examples-------->>> np.sum([0.5, 1.5])2.0>>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32)1>>> np.sum([[0, 1], [0, 5]])6>>> np.sum([[0, 1], [0, 5]], axis=0)array([0, 6])>>> np.sum([[0, 1], [0, 5]], axis=1)array([1, 5])>>> np.sum([[0, 1], [np.nan, 5]], where=[False, True], axis=1)array([1., 5.])If the accumulator is too small, overflow occurs:>>> np.ones(128, dtype=np.int8).sum(dtype=np.int8)-128You can also start the sum with a value other than zero:>>> np.sum([10], initial=5)15"""if isinstance(a, _gentype):# 2018-02-25, 1.15.0warnings.warn("Calling np.sum(generator) is deprecated, and in the future will give a different result. ""Use np.sum(np.fromiter(generator)) or the python sum builtin instead.",DeprecationWarning, stacklevel=3)res = _sum_(a)if out is not None:out[...] = resreturn outreturn resreturn _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,initial=initial, where=where)

python查看函数源代码相关推荐

  1. r语言中怎样查看函数源代码

    源自: http://zhidao.baidu.com/link?url=hp77BAOSlcZgXDFghzP8sE9Gt2D2r4YkK0cwtRzWU0EwI9w7pZ30lpEQPnJcK0_ ...

  2. matlab如何查看函数源代码,通达信fft函数,如何查看matlab fft函数源代码

    Q1:如何查看matlab fft函数源代码 n要取最接近数据长度的2的整数次方,命令是2^nextpow2(n),其中n是实际数据长度,因为这样的n可以使fft更快.比如,n=1021,执行n=2^ ...

  3. python查看函数参数_python函数参数

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 最简单的无参数def functionname(): pass function ...

  4. python查看函数参数快捷键_python查看函数源代码快捷键_pycharm中查看源码的快捷键...

    html使用心得 (1) 在 ... Spring中WebApplicationContext的研究 Spring中WebApplicationContext的研究 ApplicationContex ...

  5. r语言 c语言调用函数返回值,r语言中怎样查看函数源代码

    在R中,代码可以分为如下几个级别: ​首先,是你输入了函数对象名称,你可以直接看到代码的,如要获得函数对象fivenum的代码,就只需要在Console中键入函数对象名称fivenum就可以得到结果, ...

  6. python查看函数消耗的内存_如何查看内存占用和运行速度

    问题描述 1.Python开发的程序在使用过程中很慢,想确定下是哪段代码比较慢: 2.Python开发的程序在使用过程中占用内存很大,想确定下是哪段代码引起的: 解决方案 使用profile分析分析c ...

  7. python查看函数定义_从函数内函数定义看python的函数实现

    一.问题 在C/C++中,函数的定义本质上是在编译阶段完成,而函数调用是由链接完成.但是对于python这种语言,函数的定义和调用都是由解释器在运行时完成,或者说,解释器在执行函数定义的时候,同样是生 ...

  8. python查看函数参数,在python函数中获取参数名称列表

    Is there an easy way to be inside a python function and get a list of the parameter names? For examp ...

  9. python查看函数参数快捷键_Python基础知识—快捷键

    最重要的快捷键 1. ctrl+shift+A:万能命令行 2. shift两次:查看资源文件 新建工程第一步操作 1. module设置把空包分层去掉,compact empty middle pa ...

  10. python@内置帮助系统的使用@eval@repr@str@内置函数源代码查看

    文章目录 refs python命令行文档 python --help python help函数 进入帮助系统(简练的python文档系统) 查阅内置模块(函数/异常/对象) 内置类型 区分大小写 ...

最新文章

  1. Arm Cortex-M3 MCU性能
  2. 求表达式 f(n)的结果
  3. 小白教你一步一步安装Scrapy(西瓜皮)(带图带资源)
  4. 添加icon_Ubuntu下为AppImage应用添加图标并添加到应用
  5. 设计模式(七)适配器模式(Adapter Pattern)
  6. POJ 1511 Invitation Cards——Dijkstra优先队列优化+反向建图
  7. flex producer java_在 Logic Pro 中选取 Flex 与跟随设置
  8. C++之指针探究(一):一级指针和二级指针
  9. centos7设置静态IP地址
  10. 力扣题目系列:1. 两数之和
  11. CODEVS 3027 线段覆盖2
  12. ObjectC基础之函数调用
  13. 台式计算机怎么连手机热点,台式电脑怎么连接手机热点进行上网
  14. 经纬财富:四平怎么炒白银能挣到钱?
  15. 淘宝买家秀后台操作与各场景展示逻辑
  16. unity-粒子系统参数
  17. Linux定时器和时间管理
  18. mysql数据表操作_MySQL数据表基本操作实例详解
  19. 数据驱动运营,为门店开拓第二增长曲线。
  20. 语音相关的数据集-5个数据集

热门文章

  1. Bootstrap框架---Uploadify插件----多张图片上传交互方式一
  2. 【06年博文搬家】查看本机的瑞星序列号
  3. QT QAudioOutput+QIODevice 音频流实时播放
  4. java宠物店管理系统计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
  5. 【NDK】Android NDK下载安装教程
  6. 转臂式多芯自清洗过滤器
  7. Ubuntu 16.04 升级到内核4.18 后 vmplayer 不能运行
  8. 基于Java毕业设计房产客户信息管理系统源码+系统+mysql+lw文档+部署软件
  9. 编写c#程序,修改文件后缀名
  10. 直接选择排序到堆排序做的那些改进