Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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_Python 3.x_Inheritance_Gal - Fatal编程技术网

Python 列出类继承的所有属性

Python 列出类继承的所有属性,python,python-3.x,inheritance,gal,Python,Python 3.x,Inheritance,Gal,您好,我有下面的内容,我正试图使用StackOverflow上获得的代码从Outlook中提取数据 使用第一个循环,我试图收集对象可用的所有属性 在运行它时,我注意到缺少名称,稍后在第二个循环中会调用该名称,我假设这是由于继承。你能帮我找到一个类的所有属性吗 import win32com.client,sys o = win32com.client.gencache.EnsureDispatch("Outlook.Application") ns = o.GetNamespace("MAPI

您好,我有下面的内容,我正试图使用StackOverflow上获得的代码从Outlook中提取数据

使用第一个循环,我试图收集对象可用的所有属性

在运行它时,我注意到缺少名称,稍后在第二个循环中会调用该名称,我假设这是由于继承。你能帮我找到一个类的所有属性吗

import win32com.client,sys

o = win32com.client.gencache.EnsureDispatch("Outlook.Application")
ns = o.GetNamespace("MAPI")

adrLi = ns.AddressLists.Item("Global Address List")
contacts = adrLi.AddressEntries
numEntries = adrLi.AddressEntries.Count
print(type(contacts))
nameAliasDict = {}
attrs_ = dir(contacts)
for i in range(len(attrs_)):
    print((attrs_[i]))

for j in contacts:
    print(j.Name)

    sys.exit()
使用和内置函数

super
引用母类的实例

返回一个代理对象,该对象将方法调用委托给类型为的父类或同级类。这对于访问类中已重写的继承方法非常有用

dir
返回参数属性的列表

使用参数,尝试返回该对象的有效属性列表

输出:

[(...), 'v']
输出列表包含在母类中定义的所有属性。它主要包含从
对象继承的属性(双下划线之间的属性)


如果不扩展要了解其属性的类,只需在实例上使用
dir

>>> dir(Foo())
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'v']
dir(contacts)
将显示
联系人的所有属性名

我注意到没有名字,后来在第二个循环中被调用


您使用
j.Name
而不是
contacts.Name
,因此没有理由从第一个循环中显示
Name
。如果您想知道
j
上的所有可用属性,请在给定父类和子类的情况下执行
dir(j)
操作,如果您只需要继承的属性,则可以使用与
dir
耦合的模块:

import inspect

class Parent():
    a = 1

    def myfunc(self):
        return 2

class Child(Parent):

    c = 4


mros = inspect.getmro(Child) #returns a tuple with the class in parameter at the first position, the rest should be the parent class(es)
child_attrs = dir(mros[0])
parent_attrs = dir(mros[1])

inherited_attr = [item for item in child_attrs if item in parent_attrs]

print(parent_attrs)
print(child_attrs)
print(inherited_attr)

您可以像这样进入层次结构,但您希望Python提供一个名为dir的方便的小内置程序。我可以在类实例上使用它来获得该类的所有属性和方法的列表,以及一些继承的魔术方法,例如
\uuuuu delattr\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
\uuuuuuuuuuuuuuuu

x = dir(myClassInstance)
但你想要的是:

child.__class__.__bases__[0]().getAttributes()

\uuuu base\uuuu
是一个类属性,包含该类的基类元组。所以,如果您的类只有一个基类,这就是答案,但如果类有多个基类,则只需对该元组中的所有元素执行相同的操作。

注意对象属性可能不同,而不一定是继承;属性查找成功的名称集不一定是显式定义的。唯一可以确定的是,如果
foo.x
没有引发
AttributeError
,那么
x
属性可用于
foo
。方法
getAttributes()
从何而来?
dir(super())
没有做你认为它做的事情。例如,如果在
Bar
中定义了
v
,而不是
Foo
,它仍会显示在输出中。另外,
dir(super())
不会显示继承的方法或对象的任何方法;
dir(super())
显示的方法是一个完全不相关的列表,来自
super
本身的类层次结构。
child.__class__.__bases__[0]().getAttributes()