-1

I have a list

[<class test1.C at 0x02BAF308>, <class test1.A at 0x02BAF0D8>]

Now i need to fetch only C and A from the list and got this output by using inspect.getmro(test1.C).here A and C refer to the class name .How can i retrieve only names from that list

2 Answers2

0

If you need the names of the classes, they are stored in attribute __name__ of the class.

Minimal working example:

>>> class A(object): 
...     pass
... 
>>> import inspect
>>> inspect.getmro(A)
(<class '__main__.A'>, <type 'object'>)
>>> [c.__name__ for c in inspect.getmro(A)]
['A', 'object'] 
Andrey Sobolev
  • 12,353
  • 3
  • 48
  • 52
0

If you just want the names of the classes of the objects in a list, you can get them all fairly trivially;

the class type of any object is stored in the __class__ attribute of the class. And then all class objects have a __name__ attribute.

so;

itemClassNames = [item.__class__.__name__ for item in list]

If you just have the list of classes already, you can skip the class part, and just do

itemClassNames = [class.__name__ for class in list]
will
  • 10,260
  • 6
  • 46
  • 69