DEV Community

Keith Capers
Keith Capers

Posted on

Beginners Guide for Classes

Classes are essential. They are the blueprint for creating objects, which is the core element of OOP(Object Oriented Python). Classes help keep your code organized and are defined as a bundle of data and functionality, which can be copied and modified to accomplish a wide variety of programming tasks. Due to just how important classes are we will be looking at how to create them and a couple of pieces that help make them up.

How To create a class

Creating a class is pretty easy to do, all you will do is define it with the keyword "class" and then the name you would like to give said class followed by a colon.

class Fruit:
Enter fullscreen mode Exit fullscreen mode

Class names always start with a capital letter and if it is more than 1 word we will use UpperCamalCase. Congrats! With that you have successfully created a class.

Init and Self

The next thing your class will need is to use the init method. The init method is invoked when a class is initialized and every class has one, it comes after the "def" keyword and the word init has 2 underscores on each side, and ends with a colon.

class Fruit:

     def __init__(self):

Enter fullscreen mode Exit fullscreen mode

Init takes in arguments that are used as attributes of the class which help us customize our instances to how we want them, and this is where self comes into play. Self is a keyword that refers to the instance of a class, in the case of our Fruit class example it would be say an apple if we were creating one in python. Self also allows you to access the attributes and methods of the class.

Attributes and methods

Attributes are variables that belong to an object. For example, in our fruit class, all fruit have a name and a color so these 2 can be our attributes. When adding attributes you put them next to self as parameters and when paired with self we make it so that each new fruit we add in gets created with the name and color we assigned.

class Fruit:

     def __init__(self, name, color, brand):
         self.name = name
         self.color = color
         self.brand = brand
Enter fullscreen mode Exit fullscreen mode

But now that we have the attributes we may want the instance or fruit to do something and that's where methods come in. Methods are functions designed within a class and can be used to define the behavior of an object. Methods can access and manipulate the data attributes (variables) of the object they belong to using the self parameter. To make a method you would start it off with the def keyword and then the method name followed by a colon.

def method_name(self):
Enter fullscreen mode Exit fullscreen mode

Now that we know how to create a class and some basic parts of creating 1 let us make an instance called "favorite fruit" that will initialize with some attributes and a method to display it to everyone. When we create the instance we are going to set it up like a variable, it will be favorite_fruit = Fruit("Apple", "Red", "Granny Smith"). As you can see when we create the instance we use "Fruit" which is the name of the class, followed by () which have 3 arguments matching the attributes we set in init to initialize with. This will give the instance all of the info we set it to. After that we will print favorite_fruit.display_fruit(), If you are asking why that's a good question. After we have created the instance it is now the an instance of Fruit and because it is an instance of fruit we can call an instance method on it which in this case will be display_fruit and to invoke the method we use (). So now that we know that lets put it all together and see the finished product.

class Fruit:

    def __init__(self, name, color, brand):
        self.name = name
        self.color = color
        self.brand = brand

    def display_fruit(self):
        return f"Keith's fav fruit is: {self.name}, {self.color}, {self.brand}"



favorite_fruit = Fruit("Apple", "Red", "Granny Smith")
print(favorite_fruit.display_fruit())


Enter fullscreen mode Exit fullscreen mode

Once you run that in your terminal you will see:

Keith's fav fruit is: Apple, Red, Granny Smith
Enter fullscreen mode Exit fullscreen mode

And with that, you know some of the basic parts of a class and can do some on your own. Classes can get a lot more complicated and do a lot more than we just did here but the purpose remains the same. I am sure you can see that it does not matter how many people or how many fruit you have, once the class is created and set up the way you want it you can print as many instances of the Fruit class as you would like and can display the favorite fruits of everyone without needing to repeat a bunch of code. I hope you found this helpful thanks for reading.

Top comments (0)