{"id":417,"date":"2020-10-10T02:30:10","date_gmt":"2020-10-10T02:30:10","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=417"},"modified":"2022-07-16T08:54:26","modified_gmt":"2022-07-16T08:54:26","slug":"python-oop","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/","title":{"rendered":"Python OOP"},"content":{"rendered":"\n<p>This Python OOP explains to you the Python object-oriented programming clearly so that you can apply it to develop software more effectively.<\/p>\n\n\n\n<p>By the end of this Python OOP module, you&#8217;ll have good knowledge of object-oriented principles. And you&#8217;ll know how to use Python syntax to create reliable and robust software applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-youll-learn'>What you&#8217;ll learn <a href=\"#what-youll-learn\" class=\"anchor\" id=\"what-youll-learn\" title=\"Anchor for What you&#039;ll learn\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Create objects in Python by defining classes and methods.<\/li><li>Extend classes using inheritance.<\/li><li>SOLID principles in object-oriented programming.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='who-this-tutorial-is-for'>Who this tutorial is for? <a href=\"#who-this-tutorial-is-for\" class=\"anchor\" id=\"who-this-tutorial-is-for\" title=\"Anchor for Who this tutorial is for?\">#<\/a><\/h2>\n\n\n\n<p>If you&#8217;re new to object-oriented programming, or if you have basic Python skills and wish to learn in-depth how and when to correctly apply OOP in Python, this is the tutorial for you. <\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='classes-and-objects'>Section 1. Classes and objects <a href=\"#classes-and-objects\" class=\"anchor\" id=\"classes-and-objects\" title=\"Anchor for Section 1. Classes and objects\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-object-oriented-programming\/\">Object-oriented programming<\/a> &#8211; introduce to you the important concepts in Python object-oriented programming.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">Class<\/a>&#8211; learn how to define a class and create new objects from the class.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class-variables\/\">Class variables<\/a>&#8211; explain the class variables (or attributes)<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-methods\/\">Instance methods<\/a> &#8211; guide you on instance methods and help you understand the differences between a function and a method.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__init__\/\">__init__()<\/a>&#8211; show you how to use the __init__ method to initialize object&#8217;s attributes.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-instance-variables\/\">Instance variables<\/a> &#8211; understand the instance variables.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-private-attributes\/\">Private attributes<\/a> &#8211; learn about private attributes and how to use them effectively.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class-attributes\/\">Class attributes<\/a> &#8211; understand class attributes and more importantly when you should use class attributes.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-static-methods\/\">Static methods<\/a> &#8211; explain to you static methods and shows you how to use them to group related functions in a class.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='special-methods'>Section 2. Special methods <a href=\"#special-methods\" class=\"anchor\" id=\"special-methods\" title=\"Anchor for Section 2. Special methods\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__str__\/\">__str__ method<\/a>  &#8211; show you how to use the __str__ dunder method to return the string representation of an object.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__repr__\/\">__repr__ method<\/a> &#8211; learn how to use the __repr__ method and the main difference between __str__ and __repr__ methods.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-__eq__\/\">__eq__ method<\/a> &#8211; learn how to define the equality logic for comparing objects by values.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__hash__\/\">__hash__ method<\/a> &#8211; show you how to make a class hashable using the __has__ method.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__bool__\/\">__bool__ method<\/a> &#8211; guide you on how to determine whether a custom object is True or False using the __bool__ method.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__del__\/\">__del__ method<\/a> &#8211; understand how the __del__ method works.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='property'>Section 3. Property <a href=\"#property\" class=\"anchor\" id=\"property\" title=\"Anchor for Section 3. Property\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-properties\/\">Property<\/a>&#8211;  show you how to use the property class to create a property.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-property-decorator\/\">@property decorator<\/a> &#8211; learn how to use the @property decorator to create a property. <\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-readonly-property\/\">Read-only property<\/a> &#8211; learn how to define read-only properties and use them for computed properties.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-delete-property\/\">Delete a property<\/a> &#8211; guide you on how to delete a property from an object.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='single-inheritance'>Section 4. Single inheritance <a href=\"#single-inheritance\" class=\"anchor\" id=\"single-inheritance\" title=\"Anchor for Section 4. Single inheritance\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-inheritance\/\">Inheritance<\/a> &#8211; explain to you the inheritance concept and how to define a class that inherits from another class.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-overriding-method\/\">Overriding methods<\/a> &#8211; show you how overriding methods work.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-super\/\">super()<\/a> &#8211; learn how to delegate to the methods of the parent class from a method in the child class.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__slots__\/\">__slots__<\/a> &#8211; use __slots__ to make the class more memory efficient.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-abstract-class\/\">Abstract class<\/a> &#8211; learn what abstract classes are and how to define abstract classes.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='enumeration'>Section 5. Enumeration <a href=\"#enumeration\" class=\"anchor\" id=\"enumeration\" title=\"Anchor for Section 5. Enumeration\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-enumeration\/\">Enumeration<\/a> &#8211; show you how to define a enumeration in Python.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-enum-unique\/\">Enum Aliases &amp; @enum.unique<\/a> &#8211; introduce to you the enum aliases and how to use the enum.unique decorator to ensure the uniqueness of member values.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-enum-class\/\">Customize and extend enumerations<\/a> &#8211; learn how to customize the behaviors of enum classes and how to extend the a custom enum class.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-enum-auto\/\">auto<\/a> &#8211; use the enum <code>auto<\/code> class to generate unique values for enumeration&#8217;s members.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='solid-principles'>Section 6. SOLID principles <a href=\"#solid-principles\" class=\"anchor\" id=\"solid-principles\" title=\"Anchor for Section 6. SOLID principles\">#<\/a><\/h2>\n\n\n\n<p>This section introduces you to the SOLID principles of object-oriented programming. These five design principles make your software designs more maintainable and flexible.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-single-responsibility-principle\/\">Single Responsibility Principle<\/a> &#8211; a class should have a single resposiblity.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-open-closed-principle\/\">Open\/Closed Principle<\/a> &#8211; a class shoud be open for extension but closed for modification.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-liskov-substitution-principle\/\">Liskov Substitution Principle<\/a> &#8211; a child class must be able to substituteable for its parent class.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-interface-segregation-principle\/\">Interface Segregation Principle<\/a> &#8211; use many small interfaces instead of a big interface.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-dependency-inversion-principle\/\">Dependency Inversion Principle<\/a> &#8211; make the high-level module dependent on abstraction, not the low-level module.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='multiple-inheritance'>Section 7. Multiple inheritance <a href=\"#multiple-inheritance\" class=\"anchor\" id=\"multiple-inheritance\" title=\"Anchor for Section 7. Multiple inheritance\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-multiple-inheritance\/\">Multiple inheritance<\/a> &#8211; learn how to implement multiple inheritance and understand how the method resolution order (MRO) works in Python.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-mixin\/\">Mixin<\/a> &#8211; introduce to you the mixin concept and how to implement mixin in Python.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='descriptors'>Section 8. Descriptors <a href=\"#descriptors\" class=\"anchor\" id=\"descriptors\" title=\"Anchor for Section 8. Descriptors\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-descriptors\/\">Descriptors<\/a> &#8211; explain how descriptors work and how to use them to make the code reusable.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-data-descriptors\/\">Data vs non-data descriptors<\/a> &#8211; understand the differences between data and non-data descriptors<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='metaprogramming'>Section 9. Metaprogramming <a href=\"#metaprogramming\" class=\"anchor\" id=\"metaprogramming\" title=\"Anchor for Section 9. Metaprogramming\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__new__\/\">__new__<\/a> &#8211; learn how Python uses the __new__ method to create a new instance of a class.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-type-class\/\">type class<\/a> &#8211; show you how to dynamically create a class using the type class.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-metaclass\/\">Metaclass<\/a> &#8211; explain the metaclass and show you how to define a custom metaclass to create other classes.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-metaclass-example\/\">Metaclass example<\/a> &#8211; show you a metaclass example that allows you to inject many functionalities into classes.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-dataclass\/\">dataclass<\/a> &#8211; leverage dataclass to add special methods such as __init__ and __repr__ to custom classes.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='exceptions'>Section 10. Exceptions <a href=\"#exceptions\" class=\"anchor\" id=\"exceptions\" title=\"Anchor for Section 10. Exceptions\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-exceptions\/\">Exceptions<\/a> &#8211; learn about exceptions in the context of objects<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-exception-handling\/\">Exception Handling<\/a> &#8211; guide you on how to handle exceptions in the right way using the try statement.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-raise-exception\/\">Raise Exceptions<\/a> &#8211; show you how to use the raise statement to raise exceptions.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-raise-from\/\">Raise Exception from cause<\/a> &#8211; learn how to modify and forward an existing exception with a cause.<\/li><li><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-custom-exception\/\">Custom exceptions<\/a> &#8211; walk you through the steps of creating a custom exception class.<\/li><\/ul>\n<\/div><\/div>\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=\"417\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/\"\n\t\t\t\tdata-post-title=\"Python OOP\"\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=\"417\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/\"\n\t\t\t\tdata-post-title=\"Python OOP\"\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>This Python OOP explains to you the Python object-oriented programming clearly so that you can apply it to develop software more effectively.<\/p>\n","protected":false},"author":1,"featured_media":2390,"parent":0,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-417","page","type-page","status-publish","has-post-thumbnail","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/417","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=417"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/417\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media\/2390"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}