python3源代码是什么_如何用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?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。
本篇小編用BeautifulSoup庫帶大家體驗了一把查看源代碼的感覺,查詢過程中需要注意的地方也標注了出來,小伙伴們可不要出錯了哦~更多Python學習指路:PyThon學習網教學中心。
總結
以上是生活随笔為你收集整理的python3源代码是什么_如何用inspect查找python3源代码?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓checkbox无响应_【安卓手机】
- 下一篇: 怎样解决外键约束_《设计心理学》|找寻“