Difference between revisions of "Objects and Classes in Python"

From Sustainability Methods
(This article provides an introduction to classes and objects in Python.)
 
 
(One intermediate revision by one other user not shown)
Line 14: Line 14:
  
 
==Object==
 
==Object==
Objects can be understood as the entities that belong to a class. What does that mean? I think it is easier to explains it through an example.  
+
Objects can be understood as entities that belong to a class. What does that mean? I think it is easier to explain it through an example.  
  
Under the class sword, we have different kinds of swords like Iron Katana, gold broadsword, etc. So now you can see that classes are categories bundling together things (objects) based on common properties (such as sharp weapon). Each of these objects than has attributes that they either share with other objects or not.
+
Under the class sword, we have different kinds of swords like Iron Katana, gold broadsword, etc. So now you can see that classes are categories bundling together things (objects) based on common properties (such as sharp weapons). Each of these objects then has attributes that they either share with other objects or not.
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
Line 27: Line 27:
  
 
==Methods==
 
==Methods==
Methods give you a way to interact with/change/call the object. The notation that is used in Python and many other Object Oriented Programming a “dot” ( . ), which is the same as applying a function to the attribute in the object.  
+
Methods give you a way to interact with/change/call the object. The notation that is used in Python and many other Object Oriented Programming is a “dot” ( . ), which is the same as applying a function to the attribute in the object.  
  
 
Example: After a blacksmith upgrades your Iron Katana to Steel Katana, the blacksmith can then call a method .change_material(”steel”). The below example is an abstraction of how a blacksmith would change the material of my katana.
 
Example: After a blacksmith upgrades your Iron Katana to Steel Katana, the blacksmith can then call a method .change_material(”steel”). The below example is an abstraction of how a blacksmith would change the material of my katana.
Line 51: Line 51:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The “__init__” constructor is used to initialize two attributes each object in a class can have (material and shape). The "self" refers to the object that is created in the sword class. So this means, that any object in the class sword consists of the object iteself (self) and two attributes (material, shape). In the next step, we have have created a method to change the material. It defines "change_material" to change the material from the old one to the new material.
+
The “__init__” constructor is used to initialize two attributes each object in a class can have (material and shape). The "self" refers to the object that is created in the sword class. So this means, that any object in the class sword consists of the object itself (self) and two attributes (material, shape). In the next step, we have created a method to change the material. It defines "change_material" to change the material from the old one to the new material.
  
 
<syntaxhighlight lang="Python" line>
 
<syntaxhighlight lang="Python" line>
Line 59: Line 59:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The code above creates an iron katana object, and store it in variable x. Sometimes, you just forgot what is the attributes of an object, and that’s totally fine. You can just invoke the attribute name again to ask python. In the code above, to check “x”’s material and shape, you can just use x.material and x.shape.
+
The code above creates an iron katana object and stores it in variable x. Sometimes, you just forgot what is the attributes of an object, and that’s totally fine. You can just invoke the attribute name again to ask python. In the code above, to check “x”’s material and shape, you can just use x.material and x.shape.
  
 
==Quiz==
 
==Quiz==
Line 68: Line 68:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
2. Create your own oak bow with iron arrow. And don't forget to double check whether the material and arrow are correct.
+
2. Create your own oak bow with an iron arrow. And don't forget to double-check whether the material and arrow are correct.
 
<syntaxhighlight lang="Python" line>
 
<syntaxhighlight lang="Python" line>
 
my_bow = # TODO
 
my_bow = # TODO
Line 84: Line 84:
 
4. Check again your material, is it successfully upgraded?
 
4. Check again your material, is it successfully upgraded?
  
[[Category:Statistics]]
 
[[Category:Python basics]]
 
  
 
The [[Table of Contributors|author]] of this entry is XX. Edited by Milan Maushart
 
The [[Table of Contributors|author]] of this entry is XX. Edited by Milan Maushart

Latest revision as of 12:36, 3 September 2024

THIS ARTICLE IS STILL IN EDITING MODE

Introduction

In this tutorial, we will learn more about classes and objects in Python. Knowing how to use classes and objects is very handy because it allows you to bundle different entities in Pythn in a way that suits you. Have a look yourself!

Creating a Class

When talking about class, we can imagine it really as a class. For example, in many games, we can find classes in weapon types. A sword class has “attributes” such as material (can be iron, or adamantine). While a bow class has different “attributes” such as arrows, or string, it can also have the same attribute, which is material. Maybe you can already grasp here that we can treat the attribute as a variable here, with different values for sword (iron) and bow (wood).

Class Sword Bow
Attribute 1 Material Material
Attribute 2 Shape Arrow

Object

Objects can be understood as entities that belong to a class. What does that mean? I think it is easier to explain it through an example.

Under the class sword, we have different kinds of swords like Iron Katana, gold broadsword, etc. So now you can see that classes are categories bundling together things (objects) based on common properties (such as sharp weapons). Each of these objects then has attributes that they either share with other objects or not.

Sword Iron Katana gold broadsword
Material iron gold
Shape katana broadsword

Methods

Methods give you a way to interact with/change/call the object. The notation that is used in Python and many other Object Oriented Programming is a “dot” ( . ), which is the same as applying a function to the attribute in the object.

Example: After a blacksmith upgrades your Iron Katana to Steel Katana, the blacksmith can then call a method .change_material(”steel”). The below example is an abstraction of how a blacksmith would change the material of my katana.

steph_katana.material() # return iron
steph_katana.change_material("steel")
steph_katana.material() # return steel

Code Examples

Creating a Class

We can simply create a class by using “class” keyword:

class Sword:
	def __init__(self, material, shape):
		self.material = material
		self.shape = shape

	def change_material(self, new_material):
		self.material = new_material

The “__init__” constructor is used to initialize two attributes each object in a class can have (material and shape). The "self" refers to the object that is created in the sword class. So this means, that any object in the class sword consists of the object itself (self) and two attributes (material, shape). In the next step, we have created a method to change the material. It defines "change_material" to change the material from the old one to the new material.

x = Sword("iron", "katana")
x.material # to check the material
x.shape # to check the shape

The code above creates an iron katana object and stores it in variable x. Sometimes, you just forgot what is the attributes of an object, and that’s totally fine. You can just invoke the attribute name again to ask python. In the code above, to check “x”’s material and shape, you can just use x.material and x.shape.

Quiz

1. Create Bow Class that has material and arrow as attributes.

class Bow: 
	# TODO

2. Create your own oak bow with an iron arrow. And don't forget to double-check whether the material and arrow are correct.

my_bow = # TODO

3. You gave your bow to an alchemist, and he turns it into gold. Add a method/ function in the class so that the chemist can use it to change the material.

class Bow: 
	# TODO from Quiz 1

	def change_material(self, new_material):
		# TODO

4. Check again your material, is it successfully upgraded?


The author of this entry is XX. Edited by Milan Maushart