{"id":3597,"date":"2024-11-25T07:40:09","date_gmt":"2024-11-24T22:40:09","guid":{"rendered":"https:\/\/secondlife.lol\/?p=3597"},"modified":"2024-11-25T07:40:10","modified_gmt":"2024-11-24T22:40:10","slug":"mastering-python-classes-for-beginners","status":"publish","type":"post","link":"https:\/\/secondlife.lol\/en\/mastering-python-classes-for-beginners\/","title":{"rendered":"Mastering Python Classes: The Essentials of Object-Oriented Programming for Beginners"},"content":{"rendered":"<p class=\"wp-block-paragraph\">hello, <a href=\"https:\/\/www.python.org\/\" data-type=\"link\" data-id=\"https:\/\/www.python.org\/\" target=\"_blank\" rel=\"noopener\">Python<\/a> Lovers, today we're talking about<strong> \"Python Classes\", the flower of Python<\/strong>If you're thinking, \"Uh, classes? I heard that's hard...\" Don't worry! After reading this post, you'll be a class master in no time.<\/p>\n\n\n<style>.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{padding-top:var(--global-kb-spacing-sm, 1.5rem);padding-right:var(--global-kb-spacing-sm, 1.5rem);padding-bottom:var(--global-kb-spacing-sm, 1.5rem);padding-left:var(--global-kb-spacing-sm, 1.5rem);border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title-wrap{padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title-wrap{color:var(--global-palette2, #2B6CB0);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{color:var(--global-palette2, #2B6CB0);font-size:28px;font-weight:regular;font-style:normal;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap .kb-table-of-content-list{color:var(--global-palette1, #3182CE);line-height:2em;font-weight:regular;font-style:normal;margin-top:var(--global-kb-spacing-sm, 1.5rem);margin-right:0px;margin-bottom:0px;margin-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap .kb-table-of-content-list .kb-table-of-contents__entry:hover{color:var(--global-palette6, #718096);}@media all and (max-width: 1024px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);}}@media all and (max-width: 1024px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{font-size:28px;}}@media all and (max-width: 767px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{font-size:28px;}}<\/style>\n\n\n<p class=\"wp-block-paragraph\">Python classes use the <strong>The core of object-oriented programming<\/strong>It sounds complicated, but it's actually a concept that's very close to our everyday lives. For example, think about your favorite puppy. Every puppy has characteristics like name, age, and breed. They also have behaviors like barking, eating, and sleeping. These characteristics and behaviors are called classes!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Python Class?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a Python class as a blueprint for creating an object. Just like we need a blueprint to build a house, we need a blueprint called a class to create an object in programming.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog:\n    def __init__(self, name, age, breed):\n        self.name = name\n        self.age = age\n        self.breed = breed\n\n    def bark(self):\n        print(f\"{self.name} is barking!\")\n\nmy_dog = Dog(\"puppy\", 3, \"golden retriever\")\nmy_dog.bark()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Code commentary:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>class Dog:<\/code>Define a class named Dog.<\/li>\n\n\n\n<li><code>def __init__(self, name, age, breed):<\/code>: The constructor method of the class. Called automatically when the object is created.<\/li>\n\n\n\n<li><code>self.name = name<\/code>: Assigns the given name value to the name property of the object.<\/li>\n\n\n\n<li><code>self.age = age<\/code>: Assigns the given age value to the object's age property.<\/li>\n\n\n\n<li><code>self.breed = breed<\/code>: Assigns the given breed value to the object's breed property.<\/li>\n\n\n\n<li><code>def bark(self):<\/code>Define a method named : bark.<\/li>\n\n\n\n<li><code>print(f\"{self.name} is barking!\")<\/code>: Outputs a barking sound with the dog's name.<\/li>\n\n\n\n<li><code>my_dog = Dog(\"puppy\", 3, \"golden retriever\")<\/code>: Create an instance of the Dog class and assign it to the my_dog variable.<\/li>\n\n\n\n<li><code>my_dog.bark()<\/code>: Call the bark method of the my_dog object.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Change the above code to <a href=\"https:\/\/www.google.com\/url?sa=t&amp;source=web&amp;rct=j&amp;opi=89978449&amp;url=https:\/\/colab.research.google.com\/&amp;ved=2ahUKEwi36I2P9_GJAxUnja8BHa5NJqQQFnoECCIQAQ&amp;usg=AOvVaw3A5aPK2kLFzKOzb6sOckVw\" target=\"_blank\" rel=\"noopener\">Google Collab<\/a>and you'll see the following result.<\/p>\n\n\n<style>.kb-image3597_d71977-76 .kb-image-has-overlay:after{opacity:0.3;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;}.kb-image3597_d71977-76 img.kb-img, .kb-image3597_d71977-76 .kb-img img{border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<div class=\"wp-block-kadence-image kb-image3597_d71977-76\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"1200\" height=\"632\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/Screenshot_20241123-162640.jpg\" alt=\"\ud30c\uc774\uc36c \ud074\ub798\uc2a4 - \uad6c\uae00 \ucf54\ub7a9\uc5d0\uc11c \uc2e4\ud589 \ud654\uba74\" class=\"kb-img wp-image-3599\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/Screenshot_20241123-162640.jpg 1200w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/Screenshot_20241123-162640-300x158.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/Screenshot_20241123-162640-600x316.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/Screenshot_20241123-162640-768x404.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/Screenshot_20241123-162640-18x9.jpg 18w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><figcaption>(Python class example - running on Google Corlab)<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Components of a class<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A Python class is made up of two main components.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Attributes<\/strong>: Variables representing characteristics of the object<\/li>\n\n\n\n<li><strong>Methods<\/strong>: Functions that represent actions an object can perform<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">In the example above, name, age, and breed are properties, and bark is a method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Utilizing Classes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let's utilize classes to create a simple game character.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class GameCharacter:\n    def __init__(self, name, level, health):\n        self.name = name\n        self.level = level\n        self.health = health\n\n    def attack(self):\n        print(f\"Attack by {self.name}! Damage {self.level * 10}\")\n\n    def take_damage(self, damage):\n        self.health -= damage\n        print(f\"{self.name} took {damage} damage, current health: {self.health}\")\n\nhero = GameCharacter(\"hero\", 5, 100)\nvillain = GameCharacter(\"villain\", 4, 80)\n\nhero.attack()\nvillain.take_damage(50)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Code commentary:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>class GameCharacter:<\/code>Define a class named GameCharacter.<\/li>\n\n\n\n<li><code>def __init__(self, name, level, health):<\/code>: The constructor method of the class.<\/li>\n\n\n\n<li><code>self.name = name<\/code>Sets the name property of the : object.<\/li>\n\n\n\n<li><code>self.level = level<\/code>Sets the level property of the : object.<\/li>\n\n\n\n<li><code>self.health = health<\/code>Sets the health property of the : object.<\/li>\n\n\n\n<li><code>def attack(self):<\/code>: Defines the attack method.<\/li>\n\n\n\n<li><code>print(f\"Attack by {self.name}! Damage {self.level * 10}\")<\/code>: Print the character's attack message.<\/li>\n\n\n\n<li><code>def take_damage(self, damage):<\/code>Defines the take_damage method.<\/li>\n\n\n\n<li><code>self.health -= damage<\/code>Decreases the character's health.<\/li>\n\n\n\n<li><code>print(f\"{self.name} took {damage} damage, current health: {self.health}\")<\/code>: Outputs the state after taking damage.<\/li>\n\n\n\n<li><code>hero = GameCharacter(\"Warrior\", 5, 100)<\/code>: Creates a hero character.<\/li>\n\n\n\n<li><code>villain = GameCharacter(\"villain\", 4, 80)<\/code>: Creates a villain character.<\/li>\n\n\n\n<li><code>hero.attack()<\/code>: Call the hero's attack method.<\/li>\n\n\n\n<li><code>villain.take_damage(50)<\/code>: Call the method that the villain takes damage from.<\/li>\n<\/ol>\n\n\n<style>.kb-image3597_149716-3b .kb-image-has-overlay:after{opacity:0.3;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;}.kb-image3597_149716-3b img.kb-img, .kb-image3597_149716-3b .kb-img img{border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<div class=\"wp-block-kadence-image kb-image3597_149716-3b\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"1200\" height=\"656\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/Screenshot_20241123-163218.jpg\" alt=\"\ud30c\uc774\uc36c \ud074\ub798\uc2a4 - \ud65c\uc6a9 \uc608\uc2dc\" class=\"kb-img wp-image-3600\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/Screenshot_20241123-163218.jpg 1200w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/Screenshot_20241123-163218-300x164.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/Screenshot_20241123-163218-600x328.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/Screenshot_20241123-163218-768x420.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/Screenshot_20241123-163218-18x10.jpg 18w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><figcaption>(Python Class Example - Game Character Creation)<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Python classes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">There are several advantages to using Python classes:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Code reusability: A class you create once can be used multiple times.<\/li>\n\n\n\n<li>Structured code: Keep related data and functions together.<\/li>\n\n\n\n<li>Extensibility: You can extend existing classes through inheritance.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, we're adding a glossary section for beginners, which will help explain some of the key terms related to Python classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Glossary of terms for beginners<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you're studying Python classes, there's a lot of unfamiliar terminology out there. Don't worry, we'll break down the key terms for you.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Classes<\/strong><br>Just as you need a blueprint to build a house, you need a blueprint to create an object: a class.<\/li>\n\n\n\n<li><strong>Object<\/strong><br>An object is an actual \"thing\" built on top of a class. If a class is a mold for a bun, an object is an actual bun made from that mold.<\/li>\n\n\n\n<li><strong>Instance<\/strong><br>It's used in a similar sense to object: objects created with a particular class are called instances of that class.<\/li>\n\n\n\n<li><strong>Attribute<\/strong><br>A variable that represents a characteristic or state that an object has. For example, \"name\" and \"age\" of a dog class are attributes.<\/li>\n\n\n\n<li><strong>Method<\/strong><br>A function that represents an action that an object can do. Things like \"bark\" and \"eat\" in a dog class are methods.<\/li>\n\n\n\n<li><strong>Constructor<\/strong><br>This is a special method that is called automatically when an object is created. In Python, the <code>__init__<\/code>for the name.<\/li>\n\n\n\n<li><strong>self<\/strong><br>This is a special parameter that points to the object itself inside a method. It's necessary when using your own properties or methods.<\/li>\n\n\n\n<li><strong>Inheritance<\/strong><br>It's the ability to extend an existing class to create a new class that inherits the characteristics of the existing class, just like you inherit the characteristics of your parents.<\/li>\n\n\n\n<li><strong>Polymorphism<\/strong><br>This property allows methods with the same name to do different things. For example, a method called \"Make Sound\" can behave differently as \"Meow\" in a dog class and \"Meow\" in a cat class.<\/li>\n\n\n\n<li><strong>Encapsulation<\/strong><br>It's a technique that hides the detailed implementation inside a class and exposes only the necessary parts to the outside world. It hides the complex internal structure and provides a simple interface.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Once you understand these terms, Python classes will feel much more familiar to you. They may seem daunting at first, but if you take them one at a time, you'll soon become a Python class master!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Finalize<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Today, we've covered the basic concepts of Python classes, from their conception to their practical use. They may have seemed intimidating at first, but now that you're more familiar with them, put them to good use and your coding skills will take off!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It's time for you to start using Python classes to create your own programs. Whether it's a puppy class, a car class, or anything else you can imagine, try it out and fall in love with it!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let's explore the world of Python together, shall we? We'll be back next time with another fun topic. Until then, happy coding!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But you don't know how to run Python code? I mentioned Google Corlab above, but if you're using a desktop at home, I recommend trying VS code. <a href=\"https:\/\/secondlife.lol\/en\/vs-code-install\/\" data-type=\"post\" data-id=\"372\">Installing VS CODE - Windows<\/a> Review the post~!<\/p>","protected":false},"excerpt":{"rendered":"<p>Hello, Python lovers! Today we're going to talk about the flower of Python, the \"Python class\"...<\/p>","protected":false},"author":3,"featured_media":3618,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[3],"tags":[505,411,504,33,506],"class_list":["post-3597","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-coding","tag-505","tag-411","tag-504","tag-33","tag-506"],"taxonomy_info":{"category":[{"value":3,"label":"\ud30c\uc774\uc36c(Python)"}],"post_tag":[{"value":505,"label":"\uac1d\uccb4\uc9c0\ud5a5 \ud504\ub85c\uadf8\ub798\ubc0d"},{"value":411,"label":"\ucf54\ub529 \ud29c\ud1a0\ub9ac\uc5bc"},{"value":504,"label":"\ud074\ub798\uc2a4"},{"value":33,"label":"\ud30c\uc774\uc36c"},{"value":506,"label":"\ud30c\uc774\uc36c \uc608\uc81c"}]},"featured_image_src_large":["https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/11\/\ud30c\uc774\uc36c-\ud074\ub798\uc2a4-\ud3ec\uc2a4\ud2b8-\uc378\ub124\uc77c-600x600.jpg",600,600,true],"author_info":{"display_name":"TERE","author_link":"https:\/\/secondlife.lol\/en\/author\/tere\/"},"comment_info":0,"category_info":[{"term_id":3,"name":"\ud30c\uc774\uc36c(Python)","slug":"python-coding","term_group":0,"term_taxonomy_id":3,"taxonomy":"category","description":"","parent":20,"count":119,"filter":"raw","cat_ID":3,"category_count":119,"category_description":"","cat_name":"\ud30c\uc774\uc36c(Python)","category_nicename":"python-coding","category_parent":20}],"tag_info":[{"term_id":505,"name":"\uac1d\uccb4\uc9c0\ud5a5 \ud504\ub85c\uadf8\ub798\ubc0d","slug":"%ea%b0%9d%ec%b2%b4%ec%a7%80%ed%96%a5-%ed%94%84%eb%a1%9c%ea%b7%b8%eb%9e%98%eb%b0%8d","term_group":0,"term_taxonomy_id":505,"taxonomy":"post_tag","description":"","parent":0,"count":4,"filter":"raw"},{"term_id":411,"name":"\ucf54\ub529 \ud29c\ud1a0\ub9ac\uc5bc","slug":"%ec%bd%94%eb%94%a9-%ed%8a%9c%ed%86%a0%eb%a6%ac%ec%96%bc","term_group":0,"term_taxonomy_id":412,"taxonomy":"post_tag","description":"","parent":0,"count":3,"filter":"raw"},{"term_id":504,"name":"\ud074\ub798\uc2a4","slug":"%ed%81%b4%eb%9e%98%ec%8a%a4","term_group":0,"term_taxonomy_id":504,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":33,"name":"\ud30c\uc774\uc36c","slug":"%ed%8c%8c%ec%9d%b4%ec%8d%ac","term_group":0,"term_taxonomy_id":33,"taxonomy":"post_tag","description":"","parent":0,"count":30,"filter":"raw"},{"term_id":506,"name":"\ud30c\uc774\uc36c \uc608\uc81c","slug":"%ed%8c%8c%ec%9d%b4%ec%8d%ac-%ec%98%88%ec%a0%9c","term_group":0,"term_taxonomy_id":506,"taxonomy":"post_tag","description":"","parent":0,"count":3,"filter":"raw"}],"_links":{"self":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/3597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/comments?post=3597"}],"version-history":[{"count":7,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/3597\/revisions"}],"predecessor-version":[{"id":3620,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/3597\/revisions\/3620"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/media\/3618"}],"wp:attachment":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/media?parent=3597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/categories?post=3597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/tags?post=3597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}