<p>Given a Python object of any kind, is there an easy way to get the list of all methods that this object has?</p>

<p>Or,</p>

<p>if this is not possible, is there at least an easy way to check if it has a particular method other than simply checking if an error occurs when the method is called?</p>

[{'answer_id': 34452, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 10, 'selected': True, 'text': '<p><strong>For many objects</strong>, you can use this code, replacing \'object\' with the object you\'re interested in:</p>\n<pre class="lang-py prettyprint-override"><code>object_methods = [method_name for method_name in dir(object)\n                  if callable(getattr(object, method_name))]\n</code></pre>\n<p>I discovered it at <a href="https://web.archive.org/web/20180901124519/http://www.diveintopython.net/power_of_introspection/index.html" rel="noreferrer">diveintopython.net</a> (now archived), that should provide some further details!</p>\n<p><strong>If you get an <code>AttributeError</code>, you can use this instead</strong>:</p>\n<p><code>getattr()</code> is intolerant of pandas style Python 3.6 abstract virtual sub-classes.  This code does the same as above and ignores exceptions.</p>\n<pre class="lang-py prettyprint-override"><code>import pandas as pd\ndf = pd.DataFrame([[10, 20, 30], [100, 200, 300]],\n                  columns=[\'foo\', \'bar\', \'baz\'])\ndef get_methods(object, spacing=20):\n  methodList = []\n  for method_name in dir(object):\n    try:\n        if callable(getattr(object, method_name)):\n            methodList.append(str(method_name))\n    except Exception:\n        methodList.append(str(method_name))\n  processFunc = (lambda s: \' \'.join(s.split())) or (lambda s: s)\n  for method in methodList:\n    try:\n        print(str(method.ljust(spacing)) + \' \' +\n              processFunc(str(getattr(object, method).__doc__)[0:90]))\n    except Exception:\n        print(method.ljust(spacing) + \' \' + \' getattr() failed\')\n\nget_methods(df[\'foo\'])\n</code></pre>\n'}, {'answer_id': 34467, 'author': 'Bill the Lizard', 'author_id': 1288, 'author_profile': 'https://Stackoverflow.com/users/1288', 'pm_score': 8, 'selected': False, 'text': '<p>You can use the built in <code>dir()</code> function to get a list of all the attributes a module has.  Try this at the command line to see how it works.</p>\n<pre><code>&gt;&gt;&gt; import moduleName\n&gt;&gt;&gt; dir(moduleName)\n</code></pre>\n<p>Also, you can use the <code>hasattr(module_name, &quot;attr_name&quot;)</code> function to find out if a module has a specific attribute.</p>\n<p>See the <a href="https://zetcode.com/python/introspection/" rel="noreferrer">Python introspection</a> for more information.</p>\n'}, {'answer_id': 34472, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': '<p>To check if it has a particular method:</p>\n\n<pre><code>hasattr(object,"method")\n</code></pre>\n'}, {'answer_id': 34481, 'author': 'jmanning2k', 'author_id': 1480, 'author_profile': 'https://Stackoverflow.com/users/1480', 'pm_score': 5, 'selected': False, 'text': '<p>On top of the more direct answers, I\'d be remiss if I didn\'t mention <a href="http://ipython.scipy.org/" rel="nofollow noreferrer">IPython</a>.</p>\n<p>Hit <kbd>Tab</kbd> to see the available methods, with autocompletion.</p>\n<p>And once you\'ve found a method, try:</p>\n<pre><code>help(object.method)\n</code></pre>\n<p>to see the pydocs, method signature, etc.</p>\n<p>Ahh... <a href="http://en.wikipedia.org/wiki/REPL" rel="nofollow noreferrer">REPL</a>.</p>\n'}, {'answer_id': 15640132, 'author': 'Bruno Bronosky', 'author_id': 117471, 'author_profile': 'https://Stackoverflow.com/users/117471', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n<p>...is there at least an easy way to check if it has a particular method other than simply checking if an error occurs when the method is called</p>\n</blockquote>\n<p>While &quot;<a href="http://docs.python.org/2/glossary.html#term-eafp" rel="nofollow noreferrer">Easier to ask for forgiveness than permission</a>&quot; is certainly the Pythonic way, you may be looking for:</p>\n<pre><code>d={\'foo\':\'bar\', \'spam\':\'eggs\'}\nif \'get\' in dir(d):\n    d.get(\'foo\')\n# OUT: \'bar\'\n</code></pre>\n'}, {'answer_id': 20100900, 'author': 'Pawan Kumar', 'author_id': 1668274, 'author_profile': 'https://Stackoverflow.com/users/1668274', 'pm_score': 7, 'selected': False, 'text': '<p>The simplest method is to use <code>dir(objectname)</code>. It will display all the methods available for that object.</p>\n'}, {'answer_id': 27380776, 'author': 'Cld', 'author_id': 3973130, 'author_profile': 'https://Stackoverflow.com/users/3973130', 'pm_score': 3, 'selected': False, 'text': '<p>The problem with all methods indicated here is that you <em>can\'t</em> be sure that a method doesn\'t exist.</p>\n<p>In Python you can intercept the dot calling through <code>__getattr__</code> and <code>__getattribute__</code>, making it possible to create method &quot;at runtime&quot;</p>\n<p>Example:</p>\n<pre><code>class MoreMethod(object):\n    def some_method(self, x):\n        return x\n    def __getattr__(self, *args):\n        return lambda x: x*2\n</code></pre>\n<p>If you execute it, you can call non-existing methods in the object dictionary...</p>\n<pre><code>&gt;&gt;&gt; o = MoreMethod()\n&gt;&gt;&gt; o.some_method(5)\n5\n&gt;&gt;&gt; dir(o)\n[\'__class__\', \'__delattr__\', \'__dict__\', \'__doc__\', \'__format__\', \'__getattr__\', \'__getattribute__\', \'__hash__\', \'__init__\', \'__module__\', \'__new__\', \'__reduce__\', \'__reduce_ex__\', \'__repr__\', \'__setattr__\', \'__sizeof__\', \'__str__\', \'__subclasshook__\', \'__weakref__\', \'some_method\']\n&gt;&gt;&gt; o.i_dont_care_of_the_name(5)\n10\n</code></pre>\n<p>And it\'s why you use the <a href="https://docs.python.org/2/glossary.html#term-eafp" rel="nofollow noreferrer">Easier to ask for forgiveness than permission</a> paradigms in Python.</p>\n'}, {'answer_id': 28220562, 'author': 'paulmelnikow', 'author_id': 893113, 'author_profile': 'https://Stackoverflow.com/users/893113', 'pm_score': 4, 'selected': False, 'text': '<p>If you specifically want <strong>methods</strong>, you should use <a href="https://docs.python.org/2/library/inspect.html#inspect.ismethod" rel="noreferrer">inspect.ismethod</a>.</p>\n\n<p>For method names:</p>\n\n<pre><code>import inspect\nmethod_names = [attr for attr in dir(self) if inspect.ismethod(getattr(self, attr))]\n</code></pre>\n\n<p>For the methods themselves:</p>\n\n<pre><code>import inspect\nmethods = [member for member in [getattr(self, attr) for attr in dir(self)] if inspect.ismethod(member)]\n</code></pre>\n\n<p>Sometimes <a href="https://docs.python.org/3/library/inspect.html#inspect.isroutine" rel="noreferrer"><code>inspect.isroutine</code></a> can be useful too (for built-ins, C extensions, Cython without the "binding" compiler directive).</p>\n'}, {'answer_id': 29851741, 'author': 'james_womack', 'author_id': 230571, 'author_profile': 'https://Stackoverflow.com/users/230571', 'pm_score': 1, 'selected': False, 'text': "<p>One can create a <code>getAttrs</code> function that will return an object's callable property names</p>\n\n<pre><code>def getAttrs(object):\n  return filter(lambda m: callable(getattr(object, m)), dir(object))\n\nprint getAttrs('Foo bar'.split(' '))\n</code></pre>\n\n<p>That'd return</p>\n\n<pre><code>['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',\n '__delslice__', '__eq__', '__format__', '__ge__', '__getattribute__', \n '__getitem__', '__getslice__', '__gt__', '__iadd__', '__imul__', '__init__', \n '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', \n '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', \n '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', \n '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', \n 'remove', 'reverse', 'sort']\n</code></pre>\n"}, {'answer_id': 32608298, 'author': 'aver', 'author_id': 3165751, 'author_profile': 'https://Stackoverflow.com/users/3165751', 'pm_score': 2, 'selected': False, 'text': '<p>There is no reliable way to list all object\'s methods. <code>dir(object)</code> is usually useful, but in some cases it may not list all methods. According to <a href="https://docs.python.org/2/library/functions.html#dir" rel="nofollow"><code>dir()</code> documentation</a>: <em>"With an argument, <strong>attempt</strong> to return a list of valid attributes for that object."</em></p>\n\n<p>Checking that method exists can be done by <code>callable(getattr(object, method))</code> as already mentioned there.</p>\n'}, {'answer_id': 37464502, 'author': 'ivanleoncz', 'author_id': 5780109, 'author_profile': 'https://Stackoverflow.com/users/5780109', 'pm_score': 5, 'selected': False, 'text': "<p>I believe that you want something like this:</p>\n<blockquote>\n<p>a list of attributes from an object</p>\n</blockquote>\n<p>The built-in function <code>dir()</code> can do this job.</p>\n<p>Taken from <code>help(dir)</code> output on your Python shell:</p>\n<blockquote>\n<p>dir(...)</p>\n<pre><code>dir([object]) -&gt; list of strings\n</code></pre>\n<p>If called without an argument, return the names in the current scope.</p>\n<p>Else, return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it.</p>\n<p>If the object supplies a method named <code>__dir__</code>, it will be used; otherwise\nthe default dir() logic is used and returns:</p>\n<ul>\n<li>for a module object: the module's attributes.</li>\n<li>for a class object:  its attributes, and recursively the attributes of its bases.</li>\n<li>for any other object: its attributes, its class's attributes, and recursively the attributes of its class's base classes.</li>\n</ul>\n</blockquote>\n<p><strong>For example:</strong></p>\n<pre><code>$ python\nPython 2.7.6 (default, Jun 22 2015, 17:58:13)\n[GCC 4.8.2] on linux2\nType &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.\n\n&gt;&gt;&gt; a = &quot;I am a string&quot;\n&gt;&gt;&gt;\n&gt;&gt;&gt; type(a)\n&lt;class 'str'&gt;\n&gt;&gt;&gt;\n&gt;&gt;&gt; dir(a)\n['__add__', '__class__', '__contains__', '__delattr__', '__doc__',\n'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',\n'__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__',\n'__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__',\n'__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__',\n'__setattr__', '__sizeof__', '__str__', '__subclasshook__',\n'_formatter_field_name_split', '_formatter_parser', 'capitalize',\n'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find',\n'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace',\n'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition',\n'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip',\n'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title',\n'translate', 'upper', 'zfill']\n</code></pre>\n"}, {'answer_id': 47317075, 'author': 'Valery Ramusik', 'author_id': 5992385, 'author_profile': 'https://Stackoverflow.com/users/5992385', 'pm_score': 4, 'selected': False, 'text': "<p>Open a Bash shell (<kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>T</kbd> on Ubuntu). Start a Python\xa03 shell in it. Create an object to observe the methods of. Just add a dot after it and press <kbd>Tab</kbd> twice and you'll see something like this:</p>\n<pre><code>user@note:~$ python3\nPython 3.4.3 (default, Nov 17 2016, 01:08:31)\n[GCC 4.8.4] on linux\nType &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.\n&gt;&gt;&gt; import readline\n&gt;&gt;&gt; readline.parse_and_bind(&quot;tab: complete&quot;)\n&gt;&gt;&gt; s = &quot;Any object. Now it's a string&quot;\n&gt;&gt;&gt; s. # here tab should be pressed twice\ns.__add__(           s.__rmod__(          s.istitle(\ns.__class__(         s.__rmul__(          s.isupper(\ns.__contains__(      s.__setattr__(       s.join(\ns.__delattr__(       s.__sizeof__(        s.ljust(\ns.__dir__(           s.__str__(           s.lower(\ns.__doc__            s.__subclasshook__(  s.lstrip(\ns.__eq__(            s.capitalize(        s.maketrans(\ns.__format__(        s.casefold(          s.partition(\ns.__ge__(            s.center(            s.replace(\ns.__getattribute__(  s.count(             s.rfind(\ns.__getitem__(       s.encode(            s.rindex(\ns.__getnewargs__(    s.endswith(          s.rjust(\ns.__gt__(            s.expandtabs(        s.rpartition(\ns.__hash__(          s.find(              s.rsplit(\ns.__init__(          s.format(            s.rstrip(\ns.__iter__(          s.format_map(        s.split(\ns.__le__(            s.index(             s.splitlines(\ns.__len__(           s.isalnum(           s.startswith(\ns.__lt__(            s.isalpha(           s.strip(\ns.__mod__(           s.isdecimal(         s.swapcase(\ns.__mul__(           s.isdigit(           s.title(\ns.__ne__(            s.isidentifier(      s.translate(\ns.__new__(           s.islower(           s.upper(\ns.__reduce__(        s.isnumeric(         s.zfill(\ns.__reduce_ex__(     s.isprintable(\ns.__repr__(          s.isspace(\n</code></pre>\n"}, {'answer_id': 48284803, 'author': 'Mahdi Ghelichi', 'author_id': 8291566, 'author_profile': 'https://Stackoverflow.com/users/8291566', 'pm_score': 1, 'selected': False, 'text': "<p>Take a list as an object</p>\n\n<pre><code>obj = []\n</code></pre>\n\n<p><code>list(filter(lambda x:callable(getattr(obj,x)),obj.__dir__()))</code></p>\n\n<p>You get:</p>\n\n<pre><code>['__add__',\n '__class__',\n '__contains__',\n '__delattr__',\n '__delitem__',\n '__dir__',\n '__eq__',\n '__format__',\n '__ge__',\n '__getattribute__',\n '__getitem__',\n '__gt__',\n '__iadd__',\n '__imul__',\n '__init__',\n '__init_subclass__',\n '__iter__',\n '__le__',\n '__len__',\n '__lt__',\n '__mul__',\n '__ne__',\n '__new__',\n '__reduce__',\n '__reduce_ex__',\n '__repr__',\n '__reversed__',\n '__rmul__',\n '__setattr__',\n '__setitem__',\n '__sizeof__',\n '__str__',\n '__subclasshook__',\n 'append',\n 'clear',\n 'copy',\n 'count',\n 'extend',\n 'index',\n 'insert',\n 'pop',\n 'remove',\n 'reverse',\n 'sort']\n</code></pre>\n"}, {'answer_id': 49194581, 'author': 'Simon PII', 'author_id': 9467669, 'author_profile': 'https://Stackoverflow.com/users/9467669', 'pm_score': 0, 'selected': False, 'text': '<p>In order to search for a specific method in a whole module</p>\n\n<pre><code>for method in dir(module) :\n  if "keyword_of_methode" in method :\n   print(method, end="\\n")\n</code></pre>\n'}, {'answer_id': 50050651, 'author': 'Paritosh Vyas', 'author_id': 6852381, 'author_profile': 'https://Stackoverflow.com/users/6852381', 'pm_score': 5, 'selected': False, 'text': '<p>The simplest way to get a list of methods of any object is to use the <code>help()</code> command.</p>\n<pre><code>help(object)\n</code></pre>\n<p>It will list out all the available/important methods associated with that object.</p>\n<p>For example:</p>\n<pre><code>help(str)\n</code></pre>\n'}, {'answer_id': 58987382, 'author': 'The Mob', 'author_id': 8812904, 'author_profile': 'https://Stackoverflow.com/users/8812904', 'pm_score': 2, 'selected': False, 'text': '<pre class="lang-py prettyprint-override"><code>import moduleName\nfor x in dir(moduleName):\n    print(x)\n</code></pre>\n<p>This should work :)</p>\n'}, {'answer_id': 61189861, 'author': 'Nick Cuevas', 'author_id': 5784892, 'author_profile': 'https://Stackoverflow.com/users/5784892', 'pm_score': 0, 'selected': False, 'text': "<p>If you are, for instance, using shell plus you can use this instead:</p>\n\n<pre><code>&gt;&gt; MyObject??\n</code></pre>\n\n<p>that way, with the '??' just after your object, it'll show you all the attributes/methods the class has.</p>\n"}, {'answer_id': 63481052, 'author': 'Sanmitha Sadhishkumar', 'author_id': 13827419, 'author_profile': 'https://Stackoverflow.com/users/13827419', 'pm_score': -1, 'selected': False, 'text': '<p>You can make use of dir() which is pre-defined in Python.</p>\n<pre><code>import module_name\ndir(module_name)\n</code></pre>\n<p>You can also pass an object to dir() as</p>\n<pre><code>dir(object_name)\n</code></pre>\n<p>If the object is an object of a pre-defined class such as int, str, etc. it displays the methods in it (you may know those methods as built in functions). If that object is created for a user-defined class, it displays all the methods given in that class.</p>\n'}, {'answer_id': 63620684, 'author': 'revliscano', 'author_id': 7303434, 'author_profile': 'https://Stackoverflow.com/users/7303434', 'pm_score': 2, 'selected': False, 'text': '<p>I have done the following function (<code>get_object_functions</code>), which receives an object (<code>object_</code>) as its argument, and <strong>returns a list (<code>functions</code>) containing all of the methods (including static and class methods) defined in the object\'s class</strong>:</p>\n<pre class="lang-py prettyprint-override"><code>def get_object_functions(object_):\n    functions = [attr_name\n                 for attr_name in dir(object_)\n                 if str(type(getattr(object_,\n                                     attr_name))) in (&quot;&lt;class \'function\'&gt;&quot;,\n                                                      &quot;&lt;class \'method\'&gt;&quot;)]\n    return functions\n</code></pre>\n<p>Well, it just checks if the string representation of the type of a class\' attribute equals <code>&quot;&lt;class \'function\'&gt;&quot;</code> or <code>&quot;&lt;class \'method\'&gt;&quot;</code> and then includes that attribute in the <code>functions</code> list if that\'s <code>True</code>.</p>\n<hr />\n<h3>Demo</h3>\n<pre class="lang-py prettyprint-override"><code>class Person:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n    def introduce(self):\n        print(f\'My name is {self.name}\')\n\n    @staticmethod\n    def say_hi():\n        print(\'hi\')\n\n    @classmethod\n    def reproduce(cls, name):\n        return cls(name, 0)\n\n\nperson = Person(\'Rafael\', 27)\nprint(get_object_functions(person))\n\n</code></pre>\n<h3>Output</h3>\n<pre class="lang-py prettyprint-override"><code>[\'__init__\', \'introduce\', \'reproduce\', \'say_hi\']\n</code></pre>\n<p><em>For a cleaner version of the code:</em> <a href="https://github.com/revliscano/utilities/blob/master/get_object_functions/object_functions_getter.py" rel="nofollow noreferrer">https://github.com/revliscano/utilities/blob/master/get_object_functions/object_functions_getter.py</a></p>\n'}, {'answer_id': 65186594, 'author': 'Sergey Bushmanov', 'author_id': 4317058, 'author_profile': 'https://Stackoverflow.com/users/4317058', 'pm_score': 4, 'selected': False, 'text': '<p>Suppose we have a Python <code>obj</code>. Then to see all the methods it has, including those surrounded by <code>__</code> (<a href="https://stackoverflow.com/a/8690287/4317058">magic methods</a>):</p>\n<pre><code>print(dir(obj))\n</code></pre>\n<p>To exclude magic builtins one would do:</p>\n<pre><code>[m for m in dir(obj) if not m.startswith(\'__\')]\n</code></pre>\n'}, {'answer_id': 67662964, 'author': 'Zack Plauché', 'author_id': 10415970, 'author_profile': 'https://Stackoverflow.com/users/10415970', 'pm_score': -1, 'selected': False, 'text': "<p>Here's a nice one liner (but will get attributes as well):</p>\n<pre><code>print(*dir(obj), sep='\\n')\n</code></pre>\n"}, {'answer_id': 68474764, 'author': 'Dharma', 'author_id': 2103732, 'author_profile': 'https://Stackoverflow.com/users/2103732', 'pm_score': 0, 'selected': False, 'text': "<p>Most of the time, I want to see the user-defined methods and I don't want to see the built-in attributes that start with '__', if you want that you can use the following code:</p>\n<pre><code>object_methods = [method_name for method_name in dir(object) if callable(getattr(object, method_name)) and '__' not in method_name] \n</code></pre>\n<p>For example, for this class:</p>\n<pre><code>class Person: \n    def __init__(self, name): \n        self.name = name \n    def print_name(self):\n        print(self.name)\n</code></pre>\n<p>Above code will print: ['print_name']</p>\n"}]
['https://Stackoverflow.com/questions/34439', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3587/']