Skip to content

Instantly share code, notes, and snippets.

@ayharano
Created September 15, 2023 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayharano/82a0d19e873b2af0f84db8236d840510 to your computer and use it in GitHub Desktop.
Save ayharano/82a0d19e873b2af0f84db8236d840510 to your computer and use it in GitHub Desktop.
List methods and attributes for a Python object instance
>>> class X:
... def __init__(self):
... self._a = None
... @property
... def a(self):
... return self._a
... @a.setter
... def a(self, val):
... self._a = val
...
>>> x = X()
>>> obj = x
>>> [x for x in dir(obj) if not callable(getattr(obj, x))] # attributes
['__dict__', '__doc__', '__module__', '__weakref__', '_a', 'a']
>>> [x for x in dir(obj) if callable(getattr(obj, x))] # methods
['__class__', '__delattr__', '__dir__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment