我如何看待Python对象内部? [英] How do I look inside a Python object?
问题描述
我开始使用Python在各种项目中进行编码(包括Django Web开发和Panda3D游戏开发).
I'm starting to code in various projects using Python (including Django web development and Panda3D game development).
为了帮助我理解发生了什么,我想基本上在Python对象内部看"看它们如何打勾-像它们的方法和属性一样.
To help me understand what's going on, I would like to basically 'look' inside the Python objects to see how they tick - like their methods and properties.
所以说我有一个Python对象,我需要打印出什么内容?那有可能吗?
So say I have a Python object, what would I need to print out its contents? Is that even possible?
推荐答案
Python具有强大的自省功能.
Python has a strong set of introspection features.
看看以下内置函数:
type()
dir()
id()
getattr()
hasattr()
globals()
locals()
callable()
type()
和dir()
分别对于检查对象的类型及其属性集特别有用.
type()
and dir()
are particularly useful for inspecting the type of an object and its set of attributes, respectively.
这篇关于我如何看待Python对象内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!