Skip to content
Classes And Functions (Python)
Profile icon
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.

class <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.

class myName: name = 'Bob'

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

print(myName.name)

Output

Bob

Creating Functions Inside The Class

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

Example 1

class hello: def print_hello(): print("Hello!")

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.

hello.print_hello()

Output

Hello!

Example 2

class math: def add(a, b): return a + b

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.

print(math.add(3,5))

Output

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

8

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!

class Person: def __init__(self, name, age): self.name = name self.age = age

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.

me = Person('Bob', 12)

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.

gold = 0 owned_weapons = [] class Weapon: def __init__(self, name, cost, damage): self.name = name self.cost = cost self.sellcost = cost/2 self.damage = damage def buy(self): gold -= self.cost owned_weapons.append(self.name) def sell(self): gold += self.sellcost owned_weapons.remove(self.name)

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!

dagger = Weapon("Dagger", 5, list(range(1,3))) sword = Weapon("Sword", 15, list(range(1,6))) greatsword = Weapon("Greatsword", 30, list(range(2,13)))

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

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

sword.buy()

Output

gold

85

owned_weapons

["Sword"]

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

dict = {} class Parent: def add_word(word, definition): dict[word] = definition def remove_word(word): dict.pop(word)

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

class <student class name>(<parent class name>):

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

class some_class(Parent): pass

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!

You are viewing a single comment. View All
Profile icon
adfg3

really well explained

Profile icon
CodingElf66

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

Profile icon
adfg3

@CodingElf66
could u make a tutorial on multithreading