栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

如何用inspect查找python3源代码?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何用inspect查找python3源代码?

我们在学习的时候喜欢去写代码,或者进行代码的测试,在源代码的查看的进行的不多。大概很多是写完就放在一边,如果不是下次需要使用,也不会知道写的是否正确,还有没有可以修改或者改进的地方。所以,对于源代码的查看还是很有必要的,我们需要从发现中找到一些问题,接下来小编就教大家用inspect查找python3源代码的方法。


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

import inspect
from bs4 import BeautifulSoup


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

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 valuen',
  '      handle_endtag(name)n',
  '      handle_data(data) # Appends to the current data noden',
  '      endData(containerClass=NavigableString) # Ends the current data noden',
  'n',
  '    No matter how complicated the underlying parser is, you should ben',
  "    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。

本篇小编用BeautifulSoup库带大家体验了一把查看源代码的感觉,查询过程中需要注意的地方也标注了出来,小伙伴们可不要出错了哦~更多Python学习指路:PyThon学习网教学中心。


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/11564.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号