How to get the attributes of a class in Python

2 Answers

0 votes
class MyClassObject(object):
    pass

print(dir(MyClassObject))

'''
run:

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
'__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '__weakref__']

'''

 



answered Sep 27, 2017 by avibootz
0 votes
import inspect


class MyClassObject(object):
    pass

print(inspect.getmembers(MyClassObject))

'''
run:

[('__class__', <class 'type'>),
('__delattr__', <slot wrapper '__delattr__' of 'object' objects>),
('__dict__', mappingproxy({'__doc__': None, '__module__': '__main__',
'__weakref__': <attribute '__weakref__' of 'MyClassObject' objects>,
'__dict__': <attribute '__dict__' of 'MyClassObject' objects>})),
('__dir__', <method '__dir__' of 'object' objects>),
('__doc__', None), ('__eq__', <slot wrapper '__eq__' of 'object' objects>),
('__format__', <method '__format__' of 'object' objects>),
('__ge__', <slot wrapper '__ge__' of 'object' objects>),
('__getattribute__', <slot wrapper '__getattribute__' of 'object' objects>), 
('__gt__', <slot wrapper '__gt__' of 'object' objects>),
('__hash__', <slot wrapper '__hash__' of 'object' objects>),
('__init__', <slot wrapper '__init__' of 'object' objects>),
('__le__', <slot wrapper '__le__' of 'object' objects>),
('__lt__', <slot wrapper '__lt__' of 'object' objects>),
('__module__', '__main__'),
('__ne__', <slot wrapper '__ne__' of 'object' objects>),
('__new__', <built-in method __new__ of type object at 0x5258BAD0>),
('__reduce__', <method '__reduce__' of 'object' objects>),
('__reduce_ex__', <method '__reduce_ex__' of 'object' objects>),
('__repr__', <slot wrapper '__repr__' of 'object' objects>),
('__setattr__', <slot wrapper '__setattr__' of 'object' objects>),
('__sizeof__', <method '__sizeof__' of 'object' objects>),
('__str__', <slot wrapper '__str__' of 'object' objects>),
('__subclasshook__', <built-in method __subclasshook__ of type object at 0x010DB208>),
('__weakref__', <attribute '__weakref__' of 'MyClassObject' objects>)]

'''

 



answered Sep 27, 2017 by avibootz

Related questions

1 answer 228 views
2 answers 249 views
1 answer 145 views
...