{"id":594,"date":"2024-10-19T07:23:23","date_gmt":"2024-10-19T07:23:23","guid":{"rendered":"https:\/\/itsmycode.com\/?p=594"},"modified":"2024-10-19T07:23:24","modified_gmt":"2024-10-19T07:23:24","slug":"python-callable","status":"publish","type":"post","link":"https:\/\/itsmycode.com\/python-callable\/","title":{"rendered":"Python callable()"},"content":{"rendered":"\n<p>The <strong><code>callable()<\/code><\/strong> function in Python returns <strong><code>True <\/code><\/strong>if the object passed appears to be callable. Otherwise, it returns <strong><code>False<\/code><\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"callable-syntax\">callable() Syntax&nbsp;<\/h2>\n\n\n\n<p>The syntax of the <strong><code>callable()<\/code><\/strong> method is&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>callable(object)<\/strong><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"callable-parameters\">callable() Parameters<\/h2>\n\n\n\n<p>The <strong><code>callable()<\/code><\/strong> method can take only one argument, i.e., an object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"callable-return-value\">callable() Return Value<\/h2>\n\n\n\n<p>The <strong><code>callable()<\/code><\/strong> function returns<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>True <\/code><\/strong>\u2013 if the object appears callable<\/li>\n\n\n\n<li><strong><code>False <\/code><\/strong>\u2013 if the object is not callable.<\/li>\n<\/ul>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;There may be few cases where <strong><code>callable()<\/code><\/strong> returns <strong>True<\/strong>, but the call to the object may fail. But in case if <strong><code>callable()<\/code><\/strong> returns <strong>False<\/strong>, the calling object will never succeed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-1-how-does-callable-works\"><strong>Example 1:<\/strong>&nbsp;How does callable() works?<\/h2>\n\n\n\n<p>Here, the object&nbsp;<strong><code>number&nbsp;<\/code><\/strong>is not callable. And, the object&nbsp;<strong><code>getData&nbsp;<\/code><\/strong>appears to be callable (but may not be callable).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># returns false as the object of integer is not callable\nnumber = 10\nprint(callable(number))\n\ndef getData():\n  print(\"Hello World\")\n\n# returns true as the variable is callable\nobj = getData\nprint(callable(obj))<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>False\nTrue<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-2-when-an-object-is-callable\">Example 2: When an Object is callable&nbsp;<\/h2>\n\n\n\n<p>The built-in <strong><code>callable()<\/code><\/strong> method checks if the argument passed is either of the below two cases:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An instance of a class having a\u00a0<em><code><strong>__call__<\/strong><\/code><\/em>\u00a0method.<\/li>\n\n\n\n<li>It is of a type that indicates callability, such as in functions, methods, etc. or has a non-null <em><code>tp_call <\/code><\/em>(c struct) member.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Python program to demonstrate callable()\nclass Test:\n  def __call__(self):\n    print('Hello World !!!')\n\n# Suggests that Test class is callable and returns True\nprint(callable(Test))\n\n# This proves that class is callable\nTestObject = Test()\nTestObject()<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\nHello World !!!<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-3-when-an-object-is-not-callable\">Example 3: When an Object is NOT callable&nbsp;<\/h2>\n\n\n\n<p>The <strong><code>callable()<\/code><\/strong> method returns <strong>True<\/strong>, suggesting that the&nbsp;<strong>Test<\/strong>&nbsp;class is callable, but the instance of&nbsp;<strong>Test&nbsp;<\/strong>is not callable, and it returns a <strong>TypeError: \u2018Test\u2019 object is not callable<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python program to demonstrate NOT callable()\nclass Test:\n  def printdata(self):\n    print('Hello World !!!')\n\n# Suggests that Test class is callable and returns True\nprint(callable(Test))\n\n# The object will be created but returns error while calling\nTestObject = Test()\nTestObject()<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\n\nTraceback (most recent call last):\n  File \"c:\\Projects\\Tryouts\\main.py\", line 11, in &lt;module&gt;\n    TestObject()\nTypeError: 'Test' object is not callable<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The callable() function in Python returns True if the object passed appears to be callable. Otherwise, it returns False. callable() Syntax&nbsp; The syntax of the callable() method is&nbsp; callable(object) callable() Parameters The callable() method can take only one argument, i.e., an object. callable() Return Value The callable() function returns Note:&nbsp;There may be few cases where [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":595,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,1],"tags":[],"class_list":["post-594","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods","category-python"],"_links":{"self":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/comments?post=594"}],"version-history":[{"count":1,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/594\/revisions"}],"predecessor-version":[{"id":596,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/594\/revisions\/596"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media\/595"}],"wp:attachment":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media?parent=594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/categories?post=594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/tags?post=594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}