如何询问 python 对象的结构? [英] How to interrogate a python object about its structure?
问题描述
有没有办法以编程方式确定对象的结构/模型、它的组成部分、如何对其进行迭代、操作和转换、分解和重新构建?
Is there a way to programmatically determine an object's structure/model, it's constituent parts, how it can be iterated and manipulated and transformed, broken down and built back up?
反复试验是伟大的老师.我每天都上他们的课.但在这里,我正在寻找一种更聪明地工作"或至少以不同方式工作的方法.
Trial and error are great teachers. I take their classes every day. But here I'm looking for a way to "work smarter" or at least differently.
一点背景知识:我最近花了太多时间来错误地处理熊猫 groupby
对象,因为我不了解它的组成部分和类型.我没有正确处理在 groupby
对象上迭代时返回的元组.我现在对这个特定对象的理解稍微好一点,但还不够充分:例如,一个 groupby
obj 分解为 "params
" 和 "table
" 迭代时.table
依次由 index
和 rows
组成.但我仍然不确定如何处理 rows
:它们由什么组成或分解成什么.这篇文章包含重现问题的代码:请参阅原始文章底部的编辑 2.
A little background: I recently spent way too many hours incorrectly handling a pandas groupby
object because I did not understand the building block parts and types that it is made of. I did not correctly handle the tuple that is returned when iterating on a groupby
object. I now have a slightly better, but not at all sufficient, understanding of this particular object: e.g., a groupby
obj breaks down into "params
" and "table
" when iterated. table
, in turn, is made up of index
and rows
. But I'm still not sure how to handle rows
: what they're made of or break down into. This post includes code to reproduce the issue: see Edit 2 at the bottom of original post.
但这是一个特例;我的问题更笼统.我的问题是,如果我还不知道 python 对象/类型的结构或模型",我如何查询对象以文本方式或图形方式显示此信息?
But this is a particular case; my question is more general. My question is, if I don't already know the structure or "model" of a python object/type, how can I query the object to reveal this information textually or, even better perhaps, graphically?
编辑:
出于探索和学习的目的,下面我尝试通过 for
循环在 str
对象上运行每个 str
方法.meth
obj 在循环的前两行(例如,__add__
和 __class__
)中被正确解释,但被解释为 meth
当我尝试运行 obj.meth
时.我该如何解决这个问题?
For purposes of exploration and learning, below I try to run every str
method on a str
object via a for
loop. The meth
obj is interpreted correctly in the first two lines of the loop (e.g., __add__
and __class__
), but is interpreted as meth
when I try to run obj.meth
. How can I fix this?
输入:
obj = 'my_string'
object_methods = [method_name for method_name in dir(obj)
if callable(getattr(obj, method_name))]
print(len(object_methods))
print(object_methods)
输出:
77
['__add__', '__class__', . . ., 'upper', 'zfill']
输入:
for meth in object_methods:
print(meth)
try:
print('obj.meth for obj', obj, 'and method', meth, ':')
obj.meth
except AttributeError as e:
print('obj.meth for obj', obj, 'and method', meth, ': AttributeError:', e)
输出:
__add__
obj.meth for obj my_string and method __add__ :
obj.meth for obj my_string and method __add__ : AttributeError: 'str' object has no attribute 'meth'
__class__
obj.meth for obj my_string and method __class__ :
obj.meth for obj my_string and method __class__ : AttributeError: 'str' object has no attribute 'meth'
. . .
推荐答案
您可以使用 __dict___ 和 dir 以查看对象模型.
You can use __dict___ and dir in order to see object model.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
jack = Person('jack',23)
print(jack.__dict__)
print(dir(jack))
输出:
{'age': 23, 'name': 'jack'}
['__doc__', '__init__', '__module__', 'age', 'name']
这篇关于如何询问 python 对象的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!