LEARN PYTHON NOW! Book: The Pillars of Python. 18 — Introduction to Classes

“A friend of all is a friend of none.” Aristotle.

With what we’ve seen so far, we could do practically 95% of whatever we want with Python. However, sometimes it is better to use another paradigm.

What is a paradigm in a programming language? In our words, a paradigm is nothing but a way of thinking. That is to say, in certain languages, such as Japanese, different verbal expressions are used if we speak with someone of legal age. Although the language itself, as the idea are the same in certain cases it is best to use the respectful way.

“A person’s hands crafting pottery on a spinning wheel” by Jared Sluyter on Unsplash

Because the paradigms in programming are similar to this example, they are different ways of thinking and solving the same problem and in some cases can be more beneficial. Object-oriented programming is widely used when we have “things” that are similar to each other but have to be treated differently. For example, the POO is widely used in video games because we can have on screen an army of skeletons that share common characteristics of each other, but we need each to have its own health.

Another example is the buttons on a web page or application. These buttons can share features such as size, animations. However, when you click on each one, different actions may occur.

After this introduction of the programming concept we will see how to create a simple class.

Because the POO is something different from what we have seen so far, we need to change our thinking slightly. We are going to introduce four important concepts:

Classes are like molds.
The instances are the different skeletons or the buttons.

The self parameter is a special parameter that we will use when building the class.

The __init__ function which serves to build the class and give it the attributes (life, level of attack, level of defense)

Let’s look at an example:

#1. Creation class
class skeleton(): 
def __init__(self,life=5,attack=1,defence=2):
def scream(self):
print ("I m a Skeleton !")

#2. Creating the skeletons using the class above
print (ske3.scream())

As we can see this way of programming is conceptually different from how we have done it up to now. However, we have to think that the use of classes even though it is very widespread in python simply respond to a particular problem and that it is reduced to the reuse of code.

This is just the tip of the iceberg of the classes which can do very complex things in a few lines. However, the most important thing is to know and start playing with the tools we have seen so far (with the exception of the POO) with practical exercises, I invite you to the next chapter where we will talk about what is next.

— — — — — — — — — — — — — — The End — — — — — — — — — — — — —

If you like this small and free magazine you can help us by simply sharing it or subscribing to the publication. My name is Rubén Ruiz and I work in Artificial Intelligence in the financial industry and as a personal project I run this little magazine where we experiment with Artificial Intelligence… until the computer explodes :) You can follow me on:

Instagram (Personal life, it’s fun) => @rubenruiz_t

Youtube (Channel about AI, try to make it fun )=> Rubén Ruiz A.I.

Github (Where I upload my code, this is not so much fun anymore) => RubenRuizT

Source: Deep Learning on Medium