Home > Backend Development > Python Tutorial > Beginners Guide for Classes

Beginners Guide for Classes

Patricia Arquette
Release: 2024-11-27 02:13:14
Original
777 people have browsed it

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:
Copy after login
Copy after login

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):

Copy after login
Copy after login

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
Copy after login
Copy after login

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.

class Fruit:
Copy after login
Copy after login

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):

Copy after login
Copy after login

Once you run that in your terminal you will see:

class Fruit:

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

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.

The above is the detailed content of Beginners Guide for Classes. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template