{"id":434,"date":"2023-02-15T11:40:11","date_gmt":"2023-02-15T11:40:11","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=434"},"modified":"2023-02-15T11:49:18","modified_gmt":"2023-02-15T11:49:18","slug":"classes","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/classes\/","title":{"rendered":"Classes"},"content":{"rendered":"\n<p>Almost everything in Python is an object. Each variable is an object, but so is each function. A class allows you to group multiple objects together in logical collections to help create powerful programs.&nbsp; The class is a blueprint for creating more complex objects. Each time an instance of the class is made, it becomes a new object.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TestClass(object):\n    x = 10\n    y = 20\n\ntest_variable = TestClass()\nprint(test_variable.x)<\/code><\/pre>\n\n\n\n<p>The code above will create a new object of the TestClass class and store it in test_variable. <strong>class\u00a0<\/strong>is the keyword to define a new class, similar to how\u00a0<strong>def\u00a0<\/strong>was used to define a new function. The\u00a0<strong>object\u00a0<\/strong>word inside the brackets just means make this class based on the default Python class model so that you get all the built in features of a class.\u00a0<\/p>\n\n\n\n<p>To access class variables or functions such as x and y above you use a\u00a0<strong>.\u00a0<\/strong>after the variable storing the class such as seen on the print line in the example above. Class variables apply to\u00a0<strong>all<\/strong>\u00a0objects of that class. If you change the value of one it changes it for\u00a0<strong>all<\/strong>\u00a0of the objects. Because of this you should avoid changing class wide variables unless that is what you intend to do.<\/p>\n\n\n\n<p>The class usually contains a special function called&nbsp;<strong>__init__()<\/strong>&nbsp;which can be used to initialise variables within the instance of the class when it is created.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TestClass(object):\n    def __init__(self, first_var, second_var):\n        self.x = first_var\n        self.y = second_var\n\ntest_variable = TestClass(30, 60)\nprint(test_variable.x)<\/code><\/pre>\n\n\n\n<p>Here we can see test_variable is given some values when it is created.<\/p>\n\n\n\n<p><strong>self&nbsp;<\/strong>just refers to that instance of the class. Any function within a class has an additional variable at the start that refers to its instance. You don\u2019t have to call that variable self. You don\u2019t need to pass this variable in when calling it. This is why you see the definition line above has 3 parameters:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def __init__(self, first_var, second_var):<\/code><\/pre>\n\n\n\n<p>But the function call (or in this case class instantiation) only has 2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>test_variable = TestClass(30, 60)<\/code><\/pre>\n\n\n\n<p><strong>self.x<\/strong>&nbsp;and&nbsp;<strong>self.y<\/strong>&nbsp;are unique to this object, or instance of this class. Changing them would have no effect on a different object of the same class.<\/p>\n\n\n\n<p>You can have an __init__ function that is just to set up default values without passing in any values. Here we are just making sure x and y are specific to this instance of the class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TestClass(object):\n    def __init__(self):\n        self.x = 50\n        self.y = 100\n\ntest_variable = TestClass()\nprint(test_variable.x)<\/code><\/pre>\n\n\n\n<p>In some SQA tests you might see the word \u201cRecord\u201d being used when talking about datatypes. This simply means using a class object to store related variables in a data record.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Let\u2019s say you are making a video game and decide to store each weapon as a class object. This would allow you to have multiple variables about each weapon grouped together in a single object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Weapon(object):\n    def __init__(self, name, damage_type, damage, speed):\n        self.name = name\n        self.damage_type = damage_type\n        self.damage = damage\n        self.speed = speed\n\nshort_sword = Weapon(\"Short Sword\", \"Piercing\", 5, 10)\nbattle_axe = Weapon(\"Battle Axe\", \"Slashing\", 20, 3)\n\nprint(short_sword.name,\"does\",short_sword.damage,\"damage\")<\/code><\/pre>\n\n\n\n<p>Running this code would give an output of:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Short Sword does 5 damage<\/code><\/pre>\n\n\n\n<p>The code above created two instances of the weapon class. These objects are completely separate and have no affect on each other.<\/p>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/class-inheritance\/\" data-type=\"page\" data-id=\"447\">Next: Class inheritance<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Almost everything in Python is an object. Each variable is an object, but so is each function. A class allows you to group multiple objects together in logical collections to help create powerful programs.&nbsp; The class is a blueprint for creating more complex objects. Each time an instance of the class is made, it becomes&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/classes\/\">Continue reading <span class=\"screen-reader-text\">Classes<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":18,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-434","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/users\/5710"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/comments?post=434"}],"version-history":[{"count":11,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/434\/revisions"}],"predecessor-version":[{"id":449,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/434\/revisions\/449"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/18"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}