Tutorial Classes in Python [Tutorial]

Did this help you?

  • Yes

    Votes: 0 0.0%
  • No

    Votes: 0 0.0%
  • Kind of

    Votes: 0 0.0%

  • Total voters
    0

James

New Coder
Apr 8, 2019
21
13
4
Now, this is a massive step from the last tutorial, you should know the basics of python to understand this.
This tutorial will be going into Object Oriented Programming.

How do I define a class?
To define a class you need to type class along with the name, similar to a function it must also be followed by ():
class TestClass():
Okay, well you've defined the class, but what now? This is basically useless at this point but we can do much more.
We can make things like players, enemies, shopping carts even properties of something.

So, what else can we do?
Well, classes have things called constructors the one we will mainly use in this tutorial is __init__
Init stands for initiation, or, whenever the class is created
When creating a class it is called an Object.
Python:
class TestClass():
    def __init__(self):
        pass
So, this is how we use the __init__ constructer, it's the same as defining a function but this function is private as it starts with __
self is the object being created.

Although we have the __init__ it is still basically useless.
What we are going to do is assign some variables to the object.
Python:
class TestClass():
    def __init__(self):
        self.name = "Hello"
        self.age = 50
        self.health = 100
Now the object being created has some variables,
yet this is still useless as we haven't created the object, let's do that now.
Python:
class TestClass():
    def __init__(self):
        self.name = "Hello"
        self.age = 50
        self.health = 100

Test1 = TestClass()
Now Test1 is an object.
It does nothing, but it can now be useful.
We can change the variables for the object by simply typing
Test1.(variablename) = (newvalue)

We can also create multiple objects,
Python:
class TestClass():
    def __init__(self):
        self.name = "Hello"
        self.age = 50
        self.health = 100

Test1 = TestClass()

Test1.name = "Test1"
Test1.health = 500

Test2 = TestClass()

Test2.name = "Test2"
Test2.health = 250

print(Test1.name,Test1.health)
print(Test2.name,Test2.health)
Each object is using the same class but we have set different variables.

This can be useful for making things such as shopping carts as you can have multiple shopping cart objects.
We can do much more with classes, in this tutorial I will only show you one other thing,
We can have functions to do things in the object, to do so we define the function as normal,
Python:
class TestClass():
    def __init__(self):
        self.name = "Hello"
        self.age = 50
        self.health = 100
    def changeage(self):
        pass

Test1 = TestClass()
To run this function we can type,
Test1.changeage()
This function does nothing,
So lets make it change the age.
Python:
class TestClass():
    def __init__(self):
        self.name = "Hello"
        self.age = 50
        self.health = 100
    def changeage(self,newage):
        self.age = newage

Test1 = TestClass()
print(Test1.age)
Test1.changeage(500)
print(Test1.age)
Now if you run this it will change the age from 50 to 500.

That is all for this tutorial.

I hope this helped you!
 

Ghost

Active Coder
Moderator
Apr 19, 2019
326
149
43
I haven't had a chance to try it out, but will soon!
Thank you for sharing this :)
 
  • Like
Reactions: James