python源代码怎么用_如何查看Python函数的源代码

这两天在写code的时候,某个函数error,就想知道这个函数到底做了啥会报错。

那当想知道一些函数的源代码写了什么方法,想知道源代码放在哪个路径中,或者想将源代码进行字符操作。该咋做呢?

这时候,我们可以用很简单的方法来做Python函数源代码的查看,Python中有两个Libraries能够使用:inspect 一个Python内置的标准库

drill是一个第三方库

inspectinspect模块用于收集python对象的信息,可以获取类或函数的参数的信息,源码,解析堆栈,对对象进行类型检查等等

举个栗子,用BeautifulSoup做分析,现在我们导入了两个库

import inspect

from bs4 import BeautifulSoup

先看看BeautifulSoup的文档定义

inspect.getdoc(BeautifulSoup)

输出是:

'This class defines the basic interface called by the tree builders.\n\nThese methods will be called by the parser:\n reset()\n feed(markup)\n\nThe tree builder may call these methods from its feed() implementation:\n handle_starttag(name, attrs) # See note about return value\n handle_endtag(name)\n handle_data(data) # Appends to the current data node\n endData(containerClass=NavigableString) # Ends the current data node\n\nNo matter how complicated the underlying parser is, you should be\nable to build a tree using \'start tag\' events, \'end tag\' events,\n\'data\' events, and "done with data" events.\n\nIf you encounter an empty-element tag (aka a self-closing tag,\nlike HTML\'s
tag), call handle_starttag and then\nhandle_endtag.'

再来看看BeautifulSoup存放的路径

inspect.getsourcefile(BeautifulSoup)

输出是

'/Users/vincentyau/anaconda3/lib/python3.6/site-packages/bs4/__init__.py'

重点来了,当想看源代码的时候,可以用

inspect.getsourcelines(BeautifulSoup)

输出是

(['class BeautifulSoup(Tag):\n',

' """\n',

' This class defines the basic interface called by the tree builders.\n',

'\n',

' These methods will be called by the parser:\n',

' reset()\n',

' feed(markup)\n',

'\n',

' The tree builder may call these methods from its feed() implementation:\n',

' handle_starttag(name, attrs) # See note about return value\n',

' handle_endtag(name)\n',

' handle_data(data) # Appends to the current data node\n',

' endData(containerClass=NavigableString) # Ends the current data node\n',

'\n',

' No matter how complicated the underlying parser is, you should be\n',

" able to build a tree using 'start tag' events, 'end tag' events,\n",

' \'data\' events, and "done with data" events.\n',

'\n',

注释:太长了,我就截了一小段

如果你是用Python或者Notebook的话,inspect也可以查看你自己写的函数,用法和上面的一样。

但如果你是用terminal之类的Python编译,来查看自己定义的函数,则会引发IOError: could not get source code。

dill

刚刚发现在百度搜索dill,还搜索不出相关的结果,说明dill还是很小众,那官网的相关定义是dill extends Python's pickle module for serializing and deserializing Python objects to the majority of the built-in Python types. At the same time, it can also retrieve the source code of your Python objects. Please note dill is not a standard library, so you must install it separately.

翻译一下dill扩展了Python的pickle模块,用于序列化和反序列化Python对象到大多数内置Python类型。同时,它还可以检索Python对象的源代码。请注意dill不是标准库,所以必须单独安装它。

不过我还发现,Anaconda已经默认安装了dill(⊙v⊙)嗯

那基本的用法是

import dill

获得源代码文件路径

dill.source.getsourcefile(BeautifulSoup)

获得源代码

dill.source.getsourcelines(BeautifulSoup)

以上的输出和inspect一样,还有findsouce()函数

dill.source.findsource(BeautifulSoup)

这个的输出是

(['"""Beautiful Soup\n',

'Elixir and Tonic\n',

'"The Screen-Scraper\'s Friend"\n',

'http://www.crummy.com/software/BeautifulSoup/\n',

'\n',

'Beautiful Soup uses a pluggable XML or HTML parser to parse a\n',

'(possibly invalid) document into a tree representation. Beautiful Soup\n',

'provides methods and Pythonic idioms that make it easy to navigate,\n',

'search, and modify the parse tree.\n',

'\n',

'Beautiful Soup works with Python 2.7 and up. It works better if lxml\n',

'and/or html5lib is installed.\n',

'\n',

'For more than you ever wanted to know about Beautiful Soup, see the\n',

'documentation:\n',

'http://www.crummy.com/software/BeautifulSoup/bs4/doc/\n',

'\n',

'"""\n',

'\n',

'# Use of this source code is governed by a BSD-style license that can be\n',

'# found in the LICENSE file.\n',

注释:太长了截一段

dill还有蛮多函数的,那dill和inspect有一个大的差别就是,你用Plain Python也能用dill查看自定义的函数,而inspect则会报错

参考资料:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值