{"id":139,"date":"2024-01-25T13:37:09","date_gmt":"2024-01-25T13:37:09","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=139"},"modified":"2024-01-25T13:37:11","modified_gmt":"2024-01-25T13:37:11","slug":"python-methods","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-methods\/","title":{"rendered":"Python Methods"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python methods and the differences between functions and methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python methods<\/h2>\n\n\n\n<p>By definition, a method is a\u00a0function\u00a0that is bound to an instance of a\u00a0class. This tutorial helps you understand how it works under the hood.<\/p>\n\n\n\n<p>The following defines a\u00a0<code>Request<\/code>\u00a0class that contains a function\u00a0<code>send()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Request: def send(): print('Sent')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And you can call the\u00a0<code>send()<\/code>\u00a0function via the\u00a0<code>Request<\/code>\u00a0class like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Request.send() <em># Sent<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>send()<\/code>\u00a0is a function object, which is an instance of the\u00a0<code>function<\/code>\u00a0class as shown in the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(Request.send)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;function Request.send at 0x00000276F9E00310><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The type of the\u00a0<code>send<\/code>\u00a0is\u00a0<code>function<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(type(Request.send))<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;class 'function'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following creates a new instance of the\u00a0<code>Request<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>http_request = Request()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If you display the\u00a0<code>http_request.send<\/code>, it\u2019ll return a bound method object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(http_request.send)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;bound method Request.send of &lt;__main__.Request object at 0x00000104B6C3D580>><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>So the\u00a0<code>http_request.send<\/code>\u00a0is not a function like\u00a0<code>Request.send<\/code>. The following checks if the\u00a0<code>Request.send<\/code>\u00a0is the same object as\u00a0<code>http_request.send<\/code>. It\u2019ll returns\u00a0<code>False<\/code>\u00a0as expected:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(type(Request.send) is type(http_request.send))<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The reason is that the type of the\u00a0<code>Request.send<\/code>\u00a0is\u00a0<code>function<\/code>\u00a0while the type of the\u00a0<code>http_request.send<\/code>\u00a0is\u00a0<code>method<\/code>, as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(type(http_request.send)) <em># &lt;class 'method'><\/em> print(type(Request.send)) <em># &lt;class 'function'><\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>So when you define a function inside a class, it\u2019s purely a function. However, when you access that function via an object, the function becomes a method.<\/p>\n\n\n\n<p>Therefore, a method is a function that is bound to an instance of a class.<\/p>\n\n\n\n<p>If you call the\u00a0<code>send()<\/code>\u00a0function via the\u00a0<code>http_request<\/code>\u00a0object, you\u2019ll get a\u00a0<code>TypeError<\/code>\u00a0as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>http_request.send()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>TypeError: send() takes 0 positional arguments but 1 was given<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Because the&nbsp;<code>http_request.send<\/code>&nbsp;is a method that is bound to the&nbsp;<code>http_request<\/code>&nbsp;object, Python always implicitly passes the object to the method as the first argument.<\/p>\n\n\n\n<p>The following redefines the\u00a0<code>Request<\/code>\u00a0class where the\u00a0<code>send<\/code>\u00a0function accepts a list of arguments:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Request: def send(*args): print('Sent', args)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following calls the\u00a0<code>send<\/code>\u00a0function from the\u00a0<code>Request<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Request.send()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Sent ()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>send()<\/code>&nbsp;function doesn\u2019t receive any arguments.<\/p>\n\n\n\n<p>However, if you call the\u00a0<code>send()<\/code>\u00a0function from an instance of the\u00a0<code>Request<\/code>\u00a0class, the\u00a0<code>args<\/code>\u00a0is not empty:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>http_request.send()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Sent (&lt;__main__.Request object at 0x000001374AF4D580>,)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this case, the&nbsp;<code>send()<\/code>&nbsp;method receives an object which is the&nbsp;<code>http_request<\/code>, which is the object that it is bound to.<\/p>\n\n\n\n<p>The following illustrates that the object that calls the\u00a0<code>send()<\/code>\u00a0method is the one that Python implicitly passes into the method as the first argument:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(hex(id(http_request))) http_request.send()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>0x1ee3a74d580 Sent (&lt;__main__.Request object at 0x000001EE3A74D580>,)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>http_request<\/code>&nbsp;object is the same as the one Python passes to the&nbsp;<code>send()<\/code>&nbsp;method as the first argument because they have the same memory address. In other words, you can access the instance of the class as the first argument inside the&nbsp;<code>send()<\/code>&nbsp;method:<\/p>\n\n\n\n<p>The following method call:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>http_request.send()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>is equivalent to the following function call:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Request.send(http_request)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>For this reason, a method of an object always has the object as the first argument. By convention, it is called\u00a0<code>self<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Request: def send(self): print('Sent', self)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If you have worked with other programming languages such as Java or C#, the&nbsp;<code>self<\/code>&nbsp;is the same as the&nbsp;<code>this<\/code>&nbsp;object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python methods and the differences between functions and methods. Introduction to the Python methods By definition, a method is a\u00a0function\u00a0that is bound to an instance of a\u00a0class. This tutorial helps you understand how it works under the hood. The following defines a\u00a0Request\u00a0class that contains a function\u00a0send(): And you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-139","post","type-post","status-publish","format-standard","hentry","category-1-classes-and-objects"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=139"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/139\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/139\/revisions\/140"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}