Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何询问python对象的结构?_Python_Oop_Object - Fatal编程技术网

如何询问python对象的结构?

如何询问python对象的结构?,python,oop,object,Python,Oop,Object,是否有一种方法可以通过编程确定对象的结构/模型、组成部分、如何迭代、操作、转换、分解和构建 试错是伟大的老师。我每天都上他们的课。但在这里,我正在寻找一种“更聪明地工作”的方式,或者至少是另一种方式 一点背景:我最近花了太多时间错误地处理pandasgroupby对象,因为我不了解它的构建块部分和类型。我没有正确处理迭代groupby对象时返回的元组。我现在对这个特定对象有了稍微好一点的理解,但还不够:例如,groupbyobj在迭代时分解为“params”和“table”表依次由索引和行组成。

是否有一种方法可以通过编程确定对象的结构/模型、组成部分、如何迭代、操作、转换、分解和构建

试错是伟大的老师。我每天都上他们的课。但在这里,我正在寻找一种“更聪明地工作”的方式,或者至少是另一种方式

一点背景:我最近花了太多时间错误地处理pandas
groupby
对象,因为我不了解它的构建块部分和类型。我没有正确处理迭代
groupby
对象时返回的元组。我现在对这个特定对象有了稍微好一点的理解,但还不够:例如,
groupby
obj在迭代时分解为“
params
”和“
table
”<代码>表依次由
索引
组成。但我仍然不确定如何处理
:它们由什么组成或分解成什么。包括重现问题的代码:请参阅原始文章底部的编辑2

但这是一个特殊情况;我的问题比较笼统。我的问题是,如果我还不知道python对象/类型的结构或“模型”,我如何查询该对象,以文本方式或图形方式显示这些信息


编辑

为了探索和学习,下面我尝试通过
For
循环在
str
对象上运行每个
str
方法。
meth
obj在循环的前两行中被正确解释(例如,
\uuuuuuuuuuuu添加
\uuuuuu类
),但在我尝试运行
obj.meth
时被解释为
meth
。我怎样才能解决这个问题

输入:

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)
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)
输出:

77
['__add__', '__class__', . . ., 'upper', 'zfill']
__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'
. . .
输入:

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)
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)
输出:

77
['__add__', '__class__', . . ., 'upper', 'zfill']
__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'
. . .
您可以使用查看对象模型

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']

dir(obj)
给了我一个属性/方法列表,我以前用过。我尝试了
obj='string'
obj.\uuuu dict\uuu
并获得
AttributeError:'str'对象没有属性'\uu dict\uuu'
。它在什么环境下或针对什么类型的对象工作?来自docs:object.\uuuu dict\uuuu一个字典或其他映射对象,用于存储对象的(可写)属性。有关更多信息,请参阅。我阅读了geeks帖子,正在测试一些代码,但在原始帖子中添加了错误。你能帮我找出如何修复这个代码吗?