Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all attributes which are inherited by a class

Hi I have the below from which I am trying to pull data from Outlook using code obtained on StackOverflow.

Using the first loop, I am trying to gather all attributes available to the object.

Whilst running it I notice the absence of Name which is later called in the 2nd loop, I assume this is due to inheritance. Please can you assist me in finding all attributes available to a class?

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()
like image 721
Krishn Avatar asked May 17 '17 15:05

Krishn


People also ask

What attributes are inherited?

An attribute is said to be Inherited attribute if its parse tree node value is determined by the attribute value at parent and/or siblings node. 2. The production must have non-terminal as its head. The production must have non-terminal as a symbol in its body.

What are attributes of a class?

Class attributes are attributes which are owned by the class itself. They will be shared by all the instances of the class. Therefore they have the same value for every instance. We define class attributes outside all the methods, usually they are placed at the top, right below the class header.

Are class attributes inherited?

Objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes. The resulting classes are known as derived classes or subclasses. A subclass “inherits” all the attributes (methods, etc) of the parent class.

How do you find the attributes of a class?

Attributes of a class can also be accessed using the following built-in methods and functions : getattr() – This function is used to access the attribute of object. hasattr() – This function is used to check if an attribute exist or not. setattr() – This function is used to set an attribute.


1 Answers

Python provides a handy little builtin called dir. I can use that on a class instance to get a list of all the attributes and methods of that class along with some inherited magic methods, such as __delattr__, __dict__, __doc__, __format__, etc. You can try this yourself by doing the following:

x = dir(myClassInstance)

but what you want is:

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

__bases__ is a class attribute containing tuple of base classes for this class. so if your class has only one base class, this is the answer, but if class has more than one base class, just do same for all elements from that tuple.

like image 132
tso Avatar answered Oct 10 '22 06:10

tso