{"id":121758,"date":"2020-12-08T19:12:29","date_gmt":"2020-12-08T19:12:29","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=18037"},"modified":"2020-12-08T19:12:29","modified_gmt":"2020-12-08T19:12:29","slug":"python-callable-function","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/12\/08\/python-callable-function\/","title":{"rendered":"Python callable() Function"},"content":{"rendered":"<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"Python callable() | Wow!!! &#x1f40d;&#x1f40d; What a Great Python Trick!\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/_hKneRZRKrI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p class=\"has-pale-cyan-blue-background-color has-background\">Python&#8217;s built-in <code>callable(object)<\/code> returns <code>True<\/code> if you could call the <code>object<\/code> argument like a function with the trailing parentheses in <code>object()<\/code>. You can make any object callable by implementing the instance&#8217;s <code>__call__()<\/code> method. For example, <code>callable(callable)<\/code> returns <code>True<\/code> because <code>callable<\/code> is a function object. But <code>callable(3)<\/code> returns <code>False<\/code> because an integer is not a function you can call.<\/p>\n<p>Here&#8217;s a minimal example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> callable(callable)\nTrue\n>>> callable(3)\nFalse<\/pre>\n<p><em><strong>Note<\/strong>: The function <code>callable()<\/code> was first removed in Python 3.0 but then reintroduced in version 3.2 and above. <\/em><\/p>\n<pre class=\"wp-block-preformatted\"><strong>Syntax: <code>callable(object)<\/code><\/strong><\/pre>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><strong>Argument<\/strong><\/td>\n<td><code>object<\/code><\/td>\n<td>Any Python object such as a custom object, a list, a function, a class, or any other object. <\/td>\n<\/tr>\n<tr>\n<td><strong>Return Value<\/strong><\/td>\n<td><code>Boolean: True\/False<\/code><\/td>\n<td>Returns <code>True<\/code> if the object can be called with object()<br \/>Returns <code>False<\/code> otherwise. <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Here are some basic usages of the function:<\/p>\n<pre class=\"wp-block-preformatted\"><strong>Input <\/strong>: <code>callable(42)<\/code>\n<strong>Output <\/strong>: <code><code>False<\/code><\/code> <strong>Input <\/strong>: <code>callable(int)<\/code>\n<strong>Output <\/strong>: <code><code>True<\/code><\/code> <strong>Input <\/strong>: <code>callable(callable)<\/code>\n<strong>Output <\/strong>: <code>True<\/code><\/pre>\n<p>Want to learn more? We&#8217;re going to dive into more examples next!<\/p>\n<hr class=\"wp-block-separator\"\/>\n<p><strong>But before we move on, I&#8217;m excited to present you my brand-new Python book <a rel=\"noreferrer noopener\" href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" title=\"https:\/\/amzn.to\/2WAYeJE\">Python One-Liners<\/a><\/strong> (Amazon Link).<\/p>\n<p>If you like one-liners, you&#8217;ll LOVE the book. It&#8217;ll teach you everything there is to know about a <strong>single line of Python code.<\/strong> But it&#8217;s also an <strong>introduction to computer science<\/strong>, data science, machine learning, and algorithms. <strong><em>The universe in a single line of Python!<\/em><\/strong><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" width=\"215\" height=\"283\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/02\/image-1.png\" alt=\"\" class=\"wp-image-5969\"\/><\/a><\/figure>\n<\/div>\n<p>The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). <\/p>\n<p>Link: <a href=\"https:\/\/nostarch.com\/pythononeliners\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/nostarch.com\/pythononeliners<\/a><\/p>\n<h2>How to Check Whether a Function Object is Callable<\/h2>\n<p>The following code shows you how to use the callable() method to check whether an arbitrary object is a function, method, or another callable object: <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def finxter(): return 'Python' print(callable(finxter))\n# True f = finxter print(callable(f))\n# True print(callable(finxter()))\n# False\n<\/pre>\n<h2>How to Create Your Own Callable Object<\/h2>\n<p>The following code shows you how to create your own callable object.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Car: def __call__(self): print('brumm') porsche = Car() # Is it callable?\nprint(callable(porsche))\n# True # Call it!\nporsche()\n# brumm\n<\/pre>\n<p>This is an interesting way to make any instance instantly callable so that it can be used as a function.<\/p>\n<h2>Summary<\/h2>\n<p>Python&#8217;s built-in <code>callable(object)<\/code> returns <code>True<\/code> if you can call the <code>object<\/code> argument like a function with the trailing parentheses in <code>object()<\/code>. <\/p>\n<p>Otherwise, it returns <code>False<\/code>. <\/p>\n<p>For example, <code>callable(callable)<\/code> returns <code>True<\/code> because <code>callable<\/code> is a function object. But <code>callable(3)<\/code> returns <code>False<\/code> because an integer is not a function you can call.<\/p>\n<p>You can make any object callable by implementing the instance&#8217;s <code>__call__()<\/code> method.<\/p>\n<hr class=\"wp-block-separator\"\/>\n<p>Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n<p>The post <a href=\"https:\/\/blog.finxter.com\/python-callable-function\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python callable() Function<\/a> first appeared on <a href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Finxter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python&#8217;s built-in callable(object) returns True if you could call the object argument like a function with the trailing parentheses in object(). You can make any object callable by implementing the instance&#8217;s __call__() method. For example, callable(callable) returns True because callable is a function object. But callable(3) returns False because an integer is not a function [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-121758","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/121758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=121758"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/121758\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=121758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=121758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=121758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}