{"id":555,"date":"2020-10-15T07:25:06","date_gmt":"2020-10-15T07:25:06","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=555"},"modified":"2021-10-25T07:20:27","modified_gmt":"2021-10-25T07:20:27","slug":"python-static-methods","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-static-methods\/","title":{"rendered":"Python Static Methods"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about Python static methods and how to use them to create a utility class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-python-static-methods'>Introduction to Python static methods <a href=\"#introduction-to-python-static-methods\" class=\"anchor\" id=\"introduction-to-python-static-methods\" title=\"Anchor for Introduction to Python static methods\">#<\/a><\/h2>\n\n\n\n<p>So far, you have learned about instance methods that are bound to a specific instance. It means that instance methods can access and modify the state of the bound object.<\/p>\n\n\n\n<p>Also, you learned about <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class-methods\/\">class methods<\/a> that are bound to a class. The class methods can access and modify the class state.<\/p>\n\n\n\n<p>Unlike instance methods, static methods aren&#8217;t bound to an object. In other words, static methods cannot access and modify an object state.<\/p>\n\n\n\n<p>In addition, Python doesn&#8217;t implicitly pass the <code>cls<\/code> parameter (or the <code>self<\/code> parameter) to static methods. Therefore, static methods cannot access and modify the class&#8217;s state.<\/p>\n\n\n\n<p>In practice, you use static methods to define utility methods or group <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">functions<\/a> that have some logical relationships in a class.<\/p>\n\n\n\n<p>To define a static method, you use the <code>@staticmethod<\/code> decorator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">className<\/span>:<\/span>\n<span class=\"hljs-meta\">    @staticmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">static_method_name<\/span><span class=\"hljs-params\">(param_list)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">pass<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To call a static method, you use this syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">className.static_method_name()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='python-static-methods-vs-class-methods'>Python static methods vs class methods <a href=\"#python-static-methods-vs-class-methods\" class=\"anchor\" id=\"python-static-methods-vs-class-methods\" title=\"Anchor for Python static methods vs class methods\">#<\/a><\/h2>\n\n\n\n<p>Since static methods are quite similar to the <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class-methods\/\">class methods<\/a>, you can use the following to find the differences between them:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Class Methods<\/strong><\/th><th><strong>Static Methods<\/strong><\/th><\/tr><\/thead><tbody><tr><td>Python implicitly pass the <code>cls<\/code>  argument to class methods.<\/td><td>Python doesn&#8217;t implicitly pass the <code>cls<\/code> argument to static methods<\/td><\/tr><tr><td>Class methods <strong>can <\/strong>access and modify the class state.<\/td><td>Static methods <strong>cannot <\/strong>access or modify the class state.<\/td><\/tr><tr><td>Use <code>@classmethod<\/code> decorators to define class methods<\/td><td>Use <code>@staticmethod<\/code> decorators to define static methods.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-static-method-examples'>Python static method examples <a href=\"#python-static-method-examples\" class=\"anchor\" id=\"python-static-method-examples\" title=\"Anchor for Python static method examples\">#<\/a><\/h2>\n\n\n\n<p>The following defines a class called <code>TemperatureConverter<\/code> that has static methods for converting temperatures between Celsius, Fahrenheit, and Kelvin:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TemperatureConverter<\/span>:<\/span>\n    KEVIN = <span class=\"hljs-string\">'K'<\/span>,\n    FAHRENHEIT = <span class=\"hljs-string\">'F'<\/span>\n    CELSIUS = <span class=\"hljs-string\">'C'<\/span>\n\n<span class=\"hljs-meta\">    @staticmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">celsius_to_fahrenheit<\/span><span class=\"hljs-params\">(c)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">9<\/span>*c\/<span class=\"hljs-number\">5<\/span> + <span class=\"hljs-number\">32<\/span>\n\n<span class=\"hljs-meta\">    @staticmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fahrenheit_to_celsius<\/span><span class=\"hljs-params\">(f)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">5<\/span>*(f<span class=\"hljs-number\">-32<\/span>)\/<span class=\"hljs-number\">9<\/span>\n\n<span class=\"hljs-meta\">    @staticmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">celsius_to_kelvin<\/span><span class=\"hljs-params\">(c)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> c + <span class=\"hljs-number\">273.15<\/span>\n\n<span class=\"hljs-meta\">    @staticmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">kelvin_to_celsius<\/span><span class=\"hljs-params\">(k)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> k - <span class=\"hljs-number\">273.15<\/span>\n\n<span class=\"hljs-meta\">    @staticmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fahrenheit_to_kelvin<\/span><span class=\"hljs-params\">(f)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">5<\/span>*(f+<span class=\"hljs-number\">459.67<\/span>)\/<span class=\"hljs-number\">9<\/span>\n\n<span class=\"hljs-meta\">    @staticmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">kelvin_to_fahrenheit<\/span><span class=\"hljs-params\">(k)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">9<\/span>*k\/<span class=\"hljs-number\">5<\/span> - <span class=\"hljs-number\">459.67<\/span>\n\n<span class=\"hljs-meta\">    @staticmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">format<\/span><span class=\"hljs-params\">(value, unit)<\/span>:<\/span>\n        symbol = <span class=\"hljs-string\">''<\/span>\n        <span class=\"hljs-keyword\">if<\/span> unit == TemperatureConverter.FAHRENHEIT:\n            symbol = <span class=\"hljs-string\">'\u00b0F'<\/span>\n        <span class=\"hljs-keyword\">elif<\/span> unit == TemperatureConverter.CELSIUS:\n            symbol = <span class=\"hljs-string\">'\u00b0C'<\/span>\n        <span class=\"hljs-keyword\">elif<\/span> unit == TemperatureConverter.KEVIN:\n            symbol = <span class=\"hljs-string\">'\u00b0K'<\/span>\n\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'<span class=\"hljs-subst\">{value}<\/span><span class=\"hljs-subst\">{symbol}<\/span>'<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And to call the <code>TemperatureConverter<\/code> class, you use the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">f = TemperatureConverter.celsius_to_fahrenheit(<span class=\"hljs-number\">35<\/span>)\nprint(TemperatureConverter.format(f, TemperatureConverter.FAHRENHEIT))\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use static methods to define utility methods or group a logically related functions into a class.<\/li><li>Use the <code>@staticmethod<\/code> decorator to define a static method.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"555\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-static-methods\/\"\n\t\t\t\tdata-post-title=\"Python Static Methods\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"555\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-static-methods\/\"\n\t\t\t\tdata-post-title=\"Python Static Methods\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about Python static methods and how to use define utility methods for a class.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":9,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-555","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=555"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/555\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/417"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}