Python class

What is Python class or what is class in programming language? When questioned "What is class?" or "Why do most programmers use class?", I always answer like this:

Class is a kind of Excel table. You may know and can use tables to store data such as...

  • Human name, age, birthday
  • Stock name, price
  • City, area, population

The below "Employee" table is an axample of employees data that contains name and age columns.

Name Age
Josh 24
Kyle 51
Bob 37

So let's call the table "Employee" class and the data like (Josh, 24) "object".

  • table -> class
  • row -> object

Employee Class

Here is Employee class.

class Employee:
    def __init__(self):
        self.name = 'Josh'
        self.age = 24


s = Employee()

print(s.name)  # Josh
print(s.age)  # 24

Because the employee table has name and age columns, Employee class should have them. It's difficult to understand __init__ and self and so did I when studying. Roughly, self is an object, or a row in the table. self may be (Josh, 24) or (Kyle, 51). self itself is not data, but more of a box.

s is an object or a row. So s.name is printed Josh. . symbol calls the actual value, which is called "property" or "attribute" in Python.

Argument

We have already succeeded to get Josh data.

class Employee:
    def __init__(self):
        self.name = 'Josh'
        self.age = 24


a = Employee()
b = Employee()
c = Employee()

print(a.name)  # Josh
print(a.age)  # 24

print(b.name)  # Josh
print(b.age)  # 24

print(c.name)  # Josh
print(c.age)  # 24

a, b, c are all employees but they are all Josh. That's nonsense! How can we set Kyle or Bob data to objects?

class Employee:
    def __init__(self, worker_name, worker_age):
        self.name = worker_name
        self.age = worker_age


a = Employee('Josh', 24)
b = Employee('Kyle', 51)
c = Employee('Bob', 37)

print(a.name)  # Josh
print(a.age)  # 24

print(b.name)  # Kyle
print(b.age)  # 51

print(c.name)  # Bob
print(c.age)  # 37

There are two arguments, worker_name and worker_age, after self in __init__ function. Employee('Josh', 24) means:

worker_name = Josh
worker_age = 24

In __init__ function, self name is set to worker_name. So the name of a is set to Josh.

Country name, area, population table

Next example is the countries area and population table.

Country Area Population
France 547,557 65,311,432
Germany 348,560 83,854,376
UK 241,930 67,980,039

(Source: Countries in the world by population (2020) - worldometers)

Let's "convert" it to Python class.

class Country:
    def __init__(self, c_name, c_area, c_population):
        self.name = c_name
        self.area = c_area
        self.population = c_population


fr = Country('France', 547557, 65311432)
de = Country('Germany', 348560, 83854376)
uk = Country('UK', 241930, 67980039)

print(fr.name)  # France
print(fr.area)  # 547557
print(fr.population)  # 65311432

Now, you can understand Python class is essentially a table. Country class has 3 attributes, name, area, population. A Class attribute simply represents a table column.

Class method

Python class has functions as follows.

class Employee:
    def __init__(self, worker_name, worker_age):
        self.name = worker_name
        self.age = worker_age

    def hello(self):
        print('Hello!')


a = Employee('Josh', 24)

print(a.name)  # Josh
print(a.age)  # 24

a.hello()
# Hello!

hello is added and this simply prints Hello!. The function defined in Python class is often called "method". Method and function is almost the same. a is an Employee object ("table row") and can call hello method.

The method is so boring because all the objects print Hello!.

class Employee:
    def __init__(self, worker_name, worker_age):
        self.name = worker_name
        self.age = worker_age

    def hello(self):
        print('Hello!')


a = Employee('Josh', 24)
b = Employee('Kyle', 51)
c = Employee('Bob', 37)

a.hello()
# Hello!

b.hello()
# Hello!

c.hello()
# Hello!

Considering self.name is an employee name, hello should be changed to use self.name as follows.

class Employee:
    def __init__(self, worker_name, worker_age):
        self.name = worker_name
        self.age = worker_age

    def hello(self):
        print('I am ' + self.name + '.')


a = Employee('Josh', 24)
b = Employee('Kyle', 51)
c = Employee('Bob', 37)

a.hello()
# I am Josh.

b.hello()
# I am Kyle.

c.hello()
# I am Bob.

Why so many Python programmers use class is that a method can access the object attribute like Josh. Imagine the table of spreadsheet application. The table data can be updated by the application functions so you can calculate the sum of all columns in each row. Just like that, Python method can update object data.

class Country:
    def __init__(self, c_name, c_area, c_population):
        self.name = c_name
        self.area = c_area
        self.population = c_population
        self.density = 0.0

    def calculate_density(self):
        self.density = self.population / self.area


fr = Country('France', 547557, 65311432)
de = Country('Germany', 348560, 83854376)
uk = Country('UK', 241930, 67980039)

print(fr.density)  # 0.0

fr.calculate_density()

print(fr.density)  # 119.27786878808965

The default density is 0.0 and the density of france is initially 0.0. After running calculate_density method, the density of it is updated to 119.

Python Class

Python Tutorial