Skip to content

Introduce to class

Basic introduction to the concept of class in python.

Using class to complete data organization(数据组织)thinking

Example in real life

On a first day of the school, class teacher distributed each of a student 1 paper and let them write down their basic information like Name, Gender, Home Address, ...

  • The students may write their own information in the form of:

Form of students fill their information

Each students have their own way of writing their information details, some of them may write in form of sentences, dotted-list and others way. So it could be easily causing confusion for a teacher when facing lots of students.

In order to solve this problem. Teacher will prepared a well-structured form and print it for all of the student to fill in their details.

Prepare and print the form and let student to fill

Comapare between example in real life and programming

On a computer, we also have many different type of data container that can allow us to storing information. However if each of the students using different type of data container when they filling their information, it will also causing confusion.

Many different type to store students information

So a concept of class has been introduced, Its excatly same like the solution in real life.

  • Create a form (class)
  • Print / Distribute to students (each of the variable name use a same class)
  • Fill in the form (define the variable in a class)
  • Get the student details (Get data from a class)

Usage

class-intro.py

py
# Design / Create a student form (class)
class Student():
    name = None
    age = None
    address = None
# Print / distribute form (use class)
student_1 = Student()
student_2 = Student()

# Fill in the details (define variable)
student_1.name = "John"
student_1.age = 16
student_1.address = "UK"

student_2.name = "Ben"
student_2.age = 17
student_2.address = "US"

# Get the student details (Get data from a class)
print(student_1.name)
print(student_1.age)
print(student_1.address)

Output:

John
16
UK

Classes and Objects

In object-oriented programming (OOP), classes and objects are the two main building blocks that help you structure your code like the real world.

Event 事件 in real life

Event in real life can be sepearted into 2 category: Attributes (Data, Properties, Variable) and Behaviour (Methods)

Event in real life

Example: Example of properties and methods in real life

Properties and behaviour of a dog:

Attributes / Data / Properties / VariableBehaviour / Methods
name, age, species, fur color, weightbark, sleep, run, eat, drink

Why we need create an object to use a class ?

  • It's about a thinking of a design, An class can be considered as a "Blueprint" or a "Template" for a Building.
  • So that the building (Object) can build as planned, if building follow the design of "Blueprint" (Class).

So this kind of thinking, we called it as Object-oriented programming (OOP) 面向对象编程.

Example of using class and object

Example using clock

Properties and behaviour of a clock: Properties and behaviour of a clock

Based on the class and create an object:

Based on the class and create an object

ex-clock.py

py
class Clock:
    # Properties, Variable, Attributes
    barcodeId = None
    price = None
    frequency = 5000

    # Method, Function
    def ring(self):
        import winsound

        winsound.Beep(frequency=self.frequency, duration=100)

clock_1 = Clock()

clock_1.barcodeId = "0000001234"
clock_1.price = 5.90

clock_1.ring()

# 2nd clock

clock_2 = Clock()

clock_2.barcodeId = "ehfnefn113"
clock_2.price = 7.90
clock_2.frequency = 2000

clock_2.ring()

Output:

Bit sound and But sound