{"id":453,"date":"2023-02-15T15:58:56","date_gmt":"2023-02-15T15:58:56","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=453"},"modified":"2023-02-15T16:02:41","modified_gmt":"2023-02-15T16:02:41","slug":"methods","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/methods\/","title":{"rendered":"Methods"},"content":{"rendered":"\n<p>A method is a function within a class. You can define any number of functions within a class in the same way that you would a regular function. The only difference is the extra parameter in the definition that refers to the instance of the class, in these examples I\u2019ll call that variable&nbsp;<strong>self<\/strong>.<\/p>\n\n\n\n<p>Up until now we have just used our classes to store multiple variables in a single object. This has been a good way to keep related information in a single variable that we can pass around. Methods allow us to add functionality to classes. These functions should only be concerned with the class and not something common that might be useful for other parts of our code.<\/p>\n\n\n\n<p>Let\u2019s add a method to our class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from random import randint\n\nclass 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\n    def random_damage(self):\n        return randint(1,self.damage)\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\")\n\nclass MagicWeapon(Weapon):\n    def __init__(self, name, damage_type, damage, speed, effect):\n        super().__init__(name, damage_type, damage, speed)\n        self.effect = effect\n\nmagic_spear = MagicWeapon(\"Magic Spear\", \"Piercing\", 25, 7, \"Fire\")\nprint(magic_spear.name, \"has a magical\", magic_spear.effect, \"effect\")\n\nfor x in range(10):\n    damage = magic_spear.random_damage()\n    print(damage)<\/code><\/pre>\n\n\n\n<p>This could give an output of:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Short Sword does 5 damage\nMagic Spear has a magical\nFire effect\n14\n4\n9\n8\n21\n8\n14\n17\n18\n6<\/code><\/pre>\n\n\n\n<p>You can see that we call the method in the same way we access a variable, using a&nbsp;<strong>.<\/strong>&nbsp;after the variable name:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>damage = magic_spear.random_damage()<\/code><\/pre>\n\n\n\n<p>magic_spear is the name of the variable in which we are storing the class object, then a . then the name of the method random_damage(). Remember you are calling a function so you need the brackets even if there are no parameters to pass in.<\/p>\n\n\n\n<p><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/importing-modules-and-packages\/\" data-type=\"page\" data-id=\"458\">Next: Importing modules and packages<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A method is a function within a class. You can define any number of functions within a class in the same way that you would a regular function. The only difference is the extra parameter in the definition that refers to the instance of the class, in these examples I\u2019ll call that variable&nbsp;self. Up until&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/methods\/\">Continue reading <span class=\"screen-reader-text\">Methods<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":18,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-453","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/453","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=453"}],"version-history":[{"count":3,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/453\/revisions"}],"predecessor-version":[{"id":460,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/453\/revisions\/460"}],"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=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}