Python中,“万物皆对象”。在实际的开发中,不仅仅开发者要写很多对象,还要使用其他人编写的对象。但是,对自己不熟悉的对象,如何快速了解?看文档?速度太慢了。这里推荐一个工具:WAT。
WAT 旨在帮助开发者更轻松地检查和理解 Python 对象。它提供了直观的语法和强大的功能,能够快速查看对象的类型、属性、方法、父类型以及源代码等信息。
“Wat” is a variant of the English word “what” that is often used to express confusion or disgust.
——源自:https://igrek51.github.io/wat/
1. 安装 WAT
通过 pip
来安装:
pip install wat
安装完成后,就可以在 Python 代码中导入并使用 WAT 了。
2. 基本用法
WAT 的核心功能是通过简单的命令来检查 Python 对象。以下是一些常见的用法示例:
2.1 检查对象的类型和属性
使用 wat /
命令可以查看对象的基本信息,包括类型、属性和方法。
import wat# 检查一个字符串对象
wat / "Hello, World!"
输出示例:
value: 'hello world'
type: str
len: 11Public attributes:def capitalize() # Return a capitalized version of the string.…def casefold() # Return a version of the string suitable for caseless comparisons....
2.2 检查函数的签名
如果对某个函数的参数和返回值感兴趣,可以使用 WAT 来查看其签名。
import wat# 检查字符串的 split 方法
wat / str.split
输出示例:
value: <method 'split' of 'str' objects>
type: method_descriptor
signature: def split(self, /, sep=None, maxsplit=-1)
"""
Return a list of the substrings in the string, using sep as the separator string.sepThe separator used to split the string.When set to None (the default value), will split on any whitespacecharacter (including \n \r \t \f and spaces) and will discardempty strings from the result.maxsplitMaximum number of splits.-1 (the default value) means no limit.Splitting starts at the front of the string and works to the end.Note, str.split() is mainly useful for data that has been intentionally
delimited. With natural text that includes punctuation, consider using
the regular expression module.
"""
2.3 检查模块的结构
WAT 还可以用来探索模块的结构,包括类、函数和子模块。
import wat
import pathlib# 检查 pathlib 模块
wat / pathlib
输出示例:
value: <module 'pathlib' from '/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/pathlib/__init__.py'>
type: modulePublic attributes:class Path(*args, **kwargs) # PurePath subclass that can make system calls.…class PosixPath(*args, **kwargs) # Path subclass for non-Windows systems.…class PurePath(*args, **kwargs) # Base class for manipulating paths without I/O.…class PurePosixPath(*args, **kwargs) # PurePath subclass for non-Windows systems.…class PureWindowsPath(*args, **kwargs) # PurePath subclass for Windows systems.…class UnsupportedOperation(…) # An exception that is raised when an unsupported operation is called on…class WindowsPath(*args, **kwargs) # Path subclass for Windows systems.…Private attributes:_abc: module = <module 'pathlib._abc' from '/Library/Frameworks/Python.framework/Versions/3.13/lib/py…_local: module = <module 'pathlib._local' from '/Library/Frameworks/Python.framework/Versions/3.13/lib/…
2.4 美化嵌套数据结构
WAT 能够将复杂的嵌套数据结构(如字典)以更易读的方式展示。
import watdata = {"name": "Alice","age": 30,"address": {"city": "Wonderland","zipcode": "12345"}
}wat / data
输出示例:
{'name': 'Alice','age': 30,'address': {'city': 'Wonderland','zipcode': '12345'}
}
3. 高级用法
WAT 提供了一些修饰符,可以进一步控制输出的详细程度和格式。
3.1 使用 .short
修饰符
.short
修饰符可以生成简洁的视图,适合快速浏览。
import wat# 简洁查看列表对象
wat.short / [1,2,3]
输出示例:
value: [1,2,3,
]
type: list
len: 3
3.2 使用 .code
修饰符
.code
修饰符可以显示对象的源代码(如果可用)。
import watdef foo():return "hello world"# 查看函数的源代码
wat.code / foo
输出示例:
value: <function foo at 0x100aec720>
type: function
signature: def foo()
source code:
def foo():return "hello world"
4. 集成到开发工作流
WAT 可以无缝集成到你的开发工作流中,帮助你在调试和探索代码时更高效。例如:
- 在调试时快速查看变量的结构和内容。
- 在阅读不熟悉的代码库时,快速了解模块和类的设计。
- 在编写代码时,检查函数的签名和源代码。
5. 总结
WAT 是一个功能强大且易于使用的 Python 检查工具,适合所有级别的开发者。无论是调试、学习还是探索代码,WAT 都能为你提供清晰的洞察力。通过其灵活的语法和丰富的功能,你可以轻松回答“这是什么?”的问题,并更深入地理解 Python 对象。