Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Classes in Python [Tutorial]

James

Active Coder
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!
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom