Learn to Code via Tutorials on Repl.it!

← Back to all posts
Classes And Functions (Python)
CodingElf66

Classes And Functions

Intro

Hello, Replit Community! Today I'll be making a tutorial on Classes And Functions! I hope this tutorial helps you with your understanding of Classes And Functions in Python, and good luck with it!

Why You Should Learn Classes And Functions

Classes and Functions just make Python and coding so much easier! With it, you can do all sorts of things, such as grouping functions, creating instances, etc.. So, let's get started!

Making A Class

Making a class will be the first step, so listen carefully!

The structure for a class name is shown below.

<name> can be any variable name of your choice! For example, it can be name, player, method, etc.!

Creating Variables Inside The Class

You can create a variable inside the class, as usual.

And, to get it, we attribute the variable name to the class name!

Output

Creating Functions Inside The Class

Now, you'll learn how to create functions inside the class! There will be two examples below!

Example 1

In this first example, we set the class name to hello. We then create a function called print_hello, which just simply prints Hello! to the Python Console.

But what is important is of course, getting the function to run.

To do it, we just attribute the function to the class.

Output

Example 2

In this example, we have a function, but with parameters!

With parameters, it's still the same, but when calling the function, you just add the parameters like what you would usually do.

Output

If you're wondering what the function add will return, let's find out!

And, as usual, we get 8 (3 + 5).

__init__ Function Inside Classes

This part is a useful part in making classes, it is the __init__ function! In this part, you can assign attributes. Then, you can do self.<name> to access the attributes. So, let's get started!

In this example, we create a class called Person, and we do the __init__ function. In case you're wondering, self just is a variable that helps attributes name and age to Person.

Then, we can unlock the attributes by doing the following below.

What this line of code does just assigns the attributions. So now, me.name is equal to Bob while me.age is equal to 12.

Also, inside the function, we've just assigned self.name to Bob and self.age to 12.

Warning: self is just a variable to help with the attributes, it is not a parameter. And, self must be put in as the first parameter for __init__

Connecting __init__ To Other Functions

In the last part, we just learned that the __init__ function assigns its parameters to self.<name>. In this part, we'll connect what we learned last part with more functions!

Example 1

Let's say we're creating a whole list of weapons. But they all have their own costs, damages, etc., and we'll create some functions, such as buying functions, selling functions, etc., to make it easier.

Whoa, that's a lot here! So, let's take a huge step and explain this whole thing.

First, we create a class called Weapon.

Next, we make a __init__ function and add the parameters name, cost, and damage.

We then attribute those parameters. self.sellcost is another thing; it is just the cost divided by 2.

Next, we create two functions (buy and sell), and they just run the code that is written inside the function.

Let's create some weapons now!

So now, let's test if our functions work!

For the buy part, let's just say we have 100 gold.

Output

gold

owned_weapons

Yay! So our buy function works! For the sell one, I'm assuming that it is correct, too.

And that leaves us with our last part, Class Inheritance!

Class Inheritance

You may be wanting to create identical classes, then add on a function to the class (or whatever you want to do with the class), but Python lets you do that! It is actually pretty simple!

In Python, the class which you want to inherit from, is called the Parent Class.

Parent Class

If we want to inherit from this class, we can just do it in the structure below.

So if we want to create a class that has all the functions and things in the class Parent, we do:

That just skips the class for now, but it still inherited the functions and code inside the class Parent.

Anyways, that brings us to a stop today.

Closing

Whew, that was a big tutorial! If you found this interesting or have any feedback, then please say so in the comments below. If you need help, I'll be glad to help! Also, comment below to let me know! Anyways, hope you learned something new about Classes And Functions today, and good luck and have fun with it!

Voters
CodingElf66
SilentFoxy78
rickysong
UltimatePython
VulcanWM
Comments
hotnewtop
adfg3

really well explained

CodingElf66

@adfg3 Thanks, please leave an upvote if you found this good!

adfg3

@CodingElf66 could u make a tutorial on multithreading

VulcanWM

Nice!

CodingElf66

@VulcanWM Thanks!

VulcanWM
UltimatePython

Whoa nice! Btw I was trying to tell you the whole time, can we make a project together?