{"id":2764,"date":"2021-11-08T03:57:10","date_gmt":"2021-11-08T03:57:10","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2764"},"modified":"2025-03-28T03:06:36","modified_gmt":"2025-03-28T03:06:36","slug":"python-single-responsibility-principle","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-single-responsibility-principle\/","title":{"rendered":"Python Single Responsibility Principle"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the single responsibility principle and how to implement it in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-is-solid'>What is SOLID <a href=\"#what-is-solid\" class=\"anchor\" id=\"what-is-solid\" title=\"Anchor for What is SOLID\">#<\/a><\/h2>\n\n\n\n<p>SOLID is an abbreviation that stands for five software design principles compiled by&nbsp;<a href=\"https:\/\/blog.cleancoder.com\/\">Uncle Bob<\/a>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>S<\/strong>\u00a0&#8211; Single responsibility Principle<\/li>\n\n\n\n<li><strong>O<\/strong>\u00a0&#8211; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-open-closed-principle\/\">Open-closed Principle<\/a><\/li>\n\n\n\n<li><strong>L<\/strong>\u00a0&#8211; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-liskov-substitution-principle\/\">Liskov Substitution Principle<\/a><\/li>\n\n\n\n<li><strong>I<\/strong>\u00a0&#8211; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-interface-segregation-principle\/\">Interface Segregation Principle<\/a><\/li>\n\n\n\n<li><strong>D<\/strong>\u00a0&#8211; <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-dependency-inversion-principle\/\">Dependency Inversion Principle<\/a><\/li>\n<\/ul>\n\n\n\n<p>The single responsibility is the first principle in the SOLID principles.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-single-responsibility-principle'>Introduction to the single responsibility principle <a href=\"#introduction-to-the-single-responsibility-principle\" class=\"anchor\" id=\"introduction-to-the-single-responsibility-principle\" title=\"Anchor for Introduction to the single responsibility principle\">#<\/a><\/h2>\n\n\n\n<p>The single responsibility principle (SRP) states that every <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">class<\/a>, method, and function should have only one job or one reason to change. <\/p>\n\n\n\n<p>The purposes of the single responsibility principle are to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create high cohesive and robust classes, methods, and functions.<\/li>\n\n\n\n<li>Promote class composition<\/li>\n\n\n\n<li>Avoid code duplication<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s take a look at the following <code>Person<\/code> class:<\/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\">Person<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name)<\/span>:<\/span>\n        self.name = name\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__repr__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'Person(name=<span class=\"hljs-subst\">{self.name}<\/span>)'<\/span>\n\n<span class=\"hljs-meta\">    @classmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">save<\/span><span class=\"hljs-params\">(cls, person)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">f'Save the <span class=\"hljs-subst\">{person}<\/span> to the database'<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    p = Person(<span class=\"hljs-string\">'John Doe'<\/span>)\n    Person.save(p)<\/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><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y2xhc3MgUGVyc29uOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIG5hbWUpOgogICAgICAgIHNlbGYubmFtZSA9IG5hbWUKCiAgICBkZWYgX19yZXByX18oc2VsZik6CiAgICAgICAgcmV0dXJuIGYnUGVyc29uKG5hbWU9e3NlbGYubmFtZX0pJwoKICAgIEBjbGFzc21ldGhvZAogICAgZGVmIHNhdmUoY2xzLCBwZXJzb24pOgogICAgICAgIHByaW50KGYnU2F2ZSB0aGUge3BlcnNvbn0gdG8gdGhlIGRhdGFiYXNlJykKCgppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgcCA9IFBlcnNvbignSm9obiBEb2UnKQogICAgUGVyc29uLnNhdmUocCk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Save the Person(name=John Doe) to the database<\/code><\/span><\/pre>\n\n\n<p>This <code>Person<\/code> class has two jobs:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Manage the person&#8217;s property.<\/li>\n\n\n\n<li>Store the person in the database.<\/li>\n<\/ul>\n\n\n\n<p>Later, if you want to save the <code>Person<\/code> into different storage such as a file, you&#8217;ll need to change the <code>save()<\/code> method, which also changes the whole <code>Person<\/code> class.<\/p>\n\n\n\n<p>To make the <code>Person<\/code> class conforms to the single responsibility principle, you&#8217;ll need to create another class that is in charge of storing the <code>Person<\/code> to a database. For example:<\/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\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name)<\/span>:<\/span>\n        self.name = name\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__repr__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'Person(name=<span class=\"hljs-subst\">{self.name}<\/span>)'<\/span>\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">PersonDB<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">save<\/span><span class=\"hljs-params\">(self, person)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">f'Save the <span class=\"hljs-subst\">{person}<\/span> to the database'<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    p = Person(<span class=\"hljs-string\">'John Doe'<\/span>)\n\n    db = PersonDB()\n    db.save(p)<\/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<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y2xhc3MgUGVyc29uOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIG5hbWUpOgogICAgICAgIHNlbGYubmFtZSA9IG5hbWUKCiAgICBkZWYgX19yZXByX18oc2VsZik6CiAgICAgICAgcmV0dXJuIGYnUGVyc29uKG5hbWU9e3NlbGYubmFtZX0pJwoKCmNsYXNzIFBlcnNvbkRCOgogICAgZGVmIHNhdmUoc2VsZiwgcGVyc29uKToKICAgICAgICBwcmludChmJ1NhdmUgdGhlIHtwZXJzb259IHRvIHRoZSBkYXRhYmFzZScpCgoKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHAgPSBQZXJzb24oJ0pvaG4gRG9lJykKCiAgICBkYiA9IFBlcnNvbkRCKCkKICAgIGRiLnNhdmUocCk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this design, we separate the <code>Person<\/code> class into two classes: <code>Person<\/code> and <code>PersonDB<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Person class is responsible for managing the person&#8217;s properties.<\/li>\n\n\n\n<li>The PersonDB class is responsible for storing the person in the database.<\/li>\n<\/ul>\n\n\n\n<p>In this design, if you want to save the <code>Person<\/code> to different storage, you can define another class to do that. And you don&#8217;t need to change the <code>Person<\/code> class.<\/p>\n\n\n\n<p>When designing classes, you should put related methods that have the same reason for change together. In other words, you should separate classes if they change for different reasons.<\/p>\n\n\n\n<p>This design has one issue that you need to deal with two classes: <code>Person<\/code> and <code>PersonDB<\/code>.<\/p>\n\n\n\n<p>To make it more convenient, you can use the facade pattern so that the <code>Person<\/code> class will be the facade for the <code>PersonDB<\/code> class like this:<\/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\">PersonDB<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">save<\/span><span class=\"hljs-params\">(self, person)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">f'Save the <span class=\"hljs-subst\">{person}<\/span> to the database'<\/span>)\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name)<\/span>:<\/span>\n        self.name = name\n        self.db = PersonDB()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__repr__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'Person(name=<span class=\"hljs-subst\">{self.name}<\/span>)'<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">save<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.db.save(person=self)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    p = Person(<span class=\"hljs-string\">'John Doe'<\/span>)\n    p.save()<\/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><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y2xhc3MgUGVyc29uREI6CiAgICBkZWYgc2F2ZShzZWxmLCBwZXJzb24pOgogICAgICAgIHByaW50KGYnU2F2ZSB0aGUge3BlcnNvbn0gdG8gdGhlIGRhdGFiYXNlJykKCgpjbGFzcyBQZXJzb246CiAgICBkZWYgX19pbml0X18oc2VsZiwgbmFtZSk6CiAgICAgICAgc2VsZi5uYW1lID0gbmFtZQogICAgICAgIHNlbGYuZGIgPSBQZXJzb25EQigpCgogICAgZGVmIF9fcmVwcl9fKHNlbGYpOgogICAgICAgIHJldHVybiBmJ1BlcnNvbihuYW1lPXtzZWxmLm5hbWV9KScKCiAgICBkZWYgc2F2ZShzZWxmKToKICAgICAgICBzZWxmLmRiLnNhdmUocGVyc29uPXNlbGYpCgoKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIHAgPSBQZXJzb24oJ0pvaG4gRG9lJykKICAgIHAuc2F2ZSgp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\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\">\n<li>The single responsibility principle (SRP) states that every class, method, or function should have only one job or one reason to change.<\/li>\n\n\n\n<li>Use the single responsibility principle to separate classes, methods, and functions with the same reason for changes.<\/li>\n<\/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=\"2764\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-single-responsibility-principle\/\"\n\t\t\t\tdata-post-title=\"Python Single Responsibility Principle\"\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=\"2764\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-single-responsibility-principle\/\"\n\t\t\t\tdata-post-title=\"Python Single Responsibility Principle\"\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>Summary: in this tutorial, you&#8217;ll learn about the single responsibility principle and how to implement it in Python. What is SOLID # SOLID is an abbreviation that stands for five software design principles compiled by&nbsp;Uncle Bob: The single responsibility is the first principle in the SOLID principles. Introduction to the single responsibility principle # The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":31,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2764","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2764","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=2764"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2764\/revisions"}],"predecessor-version":[{"id":7171,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2764\/revisions\/7171"}],"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=2764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}