{"id":149,"date":"2024-01-25T13:47:04","date_gmt":"2024-01-25T13:47:04","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=149"},"modified":"2024-01-25T13:47:05","modified_gmt":"2024-01-25T13:47:05","slug":"python-static-methods","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-static-methods\/","title":{"rendered":"Python Static Methods"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll 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\">Introduction to Python static methods<\/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\u00a0class methods\u00a0that 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\u2019t 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\u2019t implicitly pass the&nbsp;<code>cls<\/code>&nbsp;parameter (or the&nbsp;<code>self<\/code>&nbsp;parameter) to static methods. Therefore, static methods cannot access and modify the class\u2019s state.<\/p>\n\n\n\n<p>In practice, you use static methods to define utility methods or group\u00a0functions\u00a0that have some logical relationships in a class.<\/p>\n\n\n\n<p>To define a static method, you use the\u00a0<code>@staticmethod<\/code>\u00a0decorator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class className: @staticmethod def static_method_name(param_list): pass<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To call a static method, you use this syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>className.static_method_name()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python static methods vs class methods<\/h2>\n\n\n\n<p>Since static methods are quite similar to the\u00a0class methods, 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&nbsp;<code>cls<\/code>&nbsp;argument to class methods.<\/td><td>Python doesn\u2019t implicitly pass the&nbsp;<code>cls<\/code>&nbsp;argument to static methods<\/td><\/tr><tr><td>Class methods&nbsp;<strong>can&nbsp;<\/strong>access and modify the class state.<\/td><td>Static methods&nbsp;<strong>cannot&nbsp;<\/strong>access or modify the class state.<\/td><\/tr><tr><td>Use&nbsp;<code>@classmethod<\/code>&nbsp;decorators to define class methods<\/td><td>Use&nbsp;<code>@staticmethod<\/code>&nbsp;decorators to define static methods.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Python static method examples<\/h2>\n\n\n\n<p>The following defines a class called\u00a0<code>TemperatureConverter<\/code>\u00a0that has static methods for converting temperatures between Celsius, Fahrenheit, and Kelvin:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class TemperatureConverter: KEVIN = 'K', FAHRENHEIT = 'F' CELSIUS = 'C' @staticmethod def celsius_to_fahrenheit(c): return 9*c\/5 + 32 @staticmethod def fahrenheit_to_celsius(f): return 5*(f-32)\/9 @staticmethod def celsius_to_kelvin(c): return c + 273.15 @staticmethod def kelvin_to_celsius(k): return k - 273.15 @staticmethod def fahrenheit_to_kelvin(f): return 5*(f+459.67)\/9 @staticmethod def kelvin_to_fahrenheit(k): return 9*k\/5 - 459.67 @staticmethod def format(value, unit): symbol = '' if unit == TemperatureConverter.FAHRENHEIT: symbol = '\u00b0F' elif unit == TemperatureConverter.CELSIUS: symbol = '\u00b0C' elif unit == TemperatureConverter.KEVIN: symbol = '\u00b0K' return f'{value}{symbol}' <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And to call the\u00a0<code>TemperatureConverter<\/code>\u00a0class, you use the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>f = TemperatureConverter.celsius_to_fahrenheit(35) print(TemperatureConverter.format(f, TemperatureConverter.FAHRENHEIT))<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python static methods and how to use them to create a utility class. Introduction to Python static methods 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. [&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-149","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\/149","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=149"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/149\/revisions"}],"predecessor-version":[{"id":150,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/149\/revisions\/150"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}