Вы можете использовать dir()
для получения всех атрибутов, хотя это будет включать методы, унаследованные от базовых классов, включая все более сложные методы объекта:
class MyClass:
def __init__(self):
self.foo = "bar"
@property
def myprop(self):
return "hello"
my_instance = MyClass()
for i in dir(my_instance):
print("object has attribute %s" % i)
print:
object has attribute __class__
object has attribute __delattr__
object has attribute __dict__
object has attribute __dir__
object has attribute __doc__
object has attribute __eq__
object has attribute __format__
object has attribute __ge__
object has attribute __getattribute__
object has attribute __gt__
object has attribute __hash__
object has attribute __init__
object has attribute __init_subclass__
object has attribute __le__
object has attribute __lt__
object has attribute __module__
object has attribute __ne__
object has attribute __new__
object has attribute __reduce__
object has attribute __reduce_ex__
object has attribute __repr__
object has attribute __setattr__
object has attribute __sizeof__
object has attribute __str__
object has attribute __subclasshook__
object has attribute __weakref__
object has attribute foo
object has attribute myprop
Вы можете исключить некоторые с помощью строковых операций:
for i in dir(my_instance):
if i.startswith("__"):
continue
print("object has attribute %s" % i)
печать:
object has attribute foo
object has attribute myprop