Classes

Almost everything in Python is an object. Each variable is an object, but so is each function. A class allows you to group multiple objects together in logical collections to help create powerful programs.  The class is a blueprint for creating more complex objects. Each time an instance of the class is made, it becomes a new object. 

class TestClass(object):
    x = 10
    y = 20

test_variable = TestClass()
print(test_variable.x)

The code above will create a new object of the TestClass class and store it in test_variable. class is the keyword to define a new class, similar to how def was used to define a new function. The object word inside the brackets just means make this class based on the default Python class model so that you get all the built in features of a class. 

To access class variables or functions such as x and y above you use a after the variable storing the class such as seen on the print line in the example above. Class variables apply to all objects of that class. If you change the value of one it changes it for all of the objects. Because of this you should avoid changing class wide variables unless that is what you intend to do.

The class usually contains a special function called __init__() which can be used to initialise variables within the instance of the class when it is created.

class TestClass(object):
    def __init__(self, first_var, second_var):
        self.x = first_var
        self.y = second_var

test_variable = TestClass(30, 60)
print(test_variable.x)

Here we can see test_variable is given some values when it is created.

self just refers to that instance of the class. Any function within a class has an additional variable at the start that refers to its instance. You don’t have to call that variable self. You don’t need to pass this variable in when calling it. This is why you see the definition line above has 3 parameters:

def __init__(self, first_var, second_var):

But the function call (or in this case class instantiation) only has 2:

test_variable = TestClass(30, 60)

self.x and self.y are unique to this object, or instance of this class. Changing them would have no effect on a different object of the same class.

You can have an __init__ function that is just to set up default values without passing in any values. Here we are just making sure x and y are specific to this instance of the class.

class TestClass(object):
    def __init__(self):
        self.x = 50
        self.y = 100

test_variable = TestClass()
print(test_variable.x)

In some SQA tests you might see the word “Record” being used when talking about datatypes. This simply means using a class object to store related variables in a data record.

Example

Let’s say you are making a video game and decide to store each weapon as a class object. This would allow you to have multiple variables about each weapon grouped together in a single object.

class Weapon(object):
    def __init__(self, name, damage_type, damage, speed):
        self.name = name
        self.damage_type = damage_type
        self.damage = damage
        self.speed = speed

short_sword = Weapon("Short Sword", "Piercing", 5, 10)
battle_axe = Weapon("Battle Axe", "Slashing", 20, 3)

print(short_sword.name,"does",short_sword.damage,"damage")

Running this code would give an output of:

Short Sword does 5 damage

The code above created two instances of the weapon class. These objects are completely separate and have no affect on each other.

Report a Glow concern
Cookie policy  Privacy policy

Glow Blogs uses cookies to enhance your experience on our service. By using this service or closing this message you consent to our use of those cookies. Please read our Cookie Policy.