Create Classes in Python
Create Classes in Python
blog
Create Classes in
Python
Hans-Petter Halvorsen
Free Textbook with lots of Practical Examples
https://www.halvorsen.blog/documents/programming/python/
Additional Python Resources
https://www.halvorsen.blog/documents/programming/python/
Contents
• Create Classes
• Use Classes
• Create a Module
• Use a Module
• Inheritance
Python Editors
• Python IDLE
• Spyder (Anaconda distribution)
• PyCharm
• Visual Studio Code
• Visual Studio
• Jupyter Notebook
• …
Spyder (Anaconda distribution)
Run Program button
Console window
Object-oriented Programming
• Python is an object-oriented programming
(OOP) language.
• Almost everything in Python is an object, with
its properties and methods.
• The foundation for all Object-oriented
Programming (OOP) languages are Classes.
• Almost all Programming Languages today use
Object-oriented Programming techniques.
Create Class
To create a class, use the keyword class: Let's create a simple Class in Python:
class ClassName: class Car:
<statement-1> model = "Volvo" Define the Class
. color = "Blue"
.
. car = Car()
<statement-N>
Use the Class
print(car.model)
print(car.color)
Create an Object of the Class
The results will be in this case:
Volvo
Blue
This example don't illustrate the good things with classes so we will create some more examples.
Classes -Example
Let's create the following class Car:
Python Code: model = "" Define the Class
color = ""
We start using the
Class by creating an car = Car()
Object of that Class
car.model = "Volvo"
car.color = "Blue"
Set Properties
print(car.color + " " + car.model)
Use the Class
car.model = "Ford"
car.color = "Green"
car1 = Car()
car2.model = "Ford"
car2.color = "Green"
print(car2.color + " " + car2.model)
The __init__() Function
In Python all classes have a built-in function called __init__(), which is always executed
when the class is being initiated.
In many other OOP languages we call this the Constructor.
def displayCar(self):
The self parameter is a print("Car: " + self.model + "-" + self.color)
reference to the current
instance of the class and # Let’s start using the Class
car1 = Car("Tesla", "Red")
is used to access variables car1.displayCar()
that belongs to the class.
car2 = Car("Ford", "Green")
car2.displayCar()
class Teacher(Person):
pass
class Student(Person): Use the pass keyword when you do not want to
pass add any other properties or methods to the class.
Inheritance
class Person: Parent Class
Inherits from
Child Class class Teacher (Person): class Student (Person): Child Class
Send the parent class as a parameter when creating the child class
Person.py
Inheritance cont. testPerson.py
class Person:
def __init__(self, name, age): from Person import *
self.name = name
self.age = age teacher1 = Teacher("Knut Nilsen", 44, "Mathematics")
teacher1.displayPerson()
def displayPerson(self):
print("Person: " + self.name + ", " + str(self.age))
student1 = Student("Per Hamsun", 20, "B")
student1.displayPerson()
class Student(Person):
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
def displayPerson(self):
print("Person: " + self.name + ", " + str(self.age) + ", " + self.grade)
class Teacher(Person):
def __init__(self, name, age, course):
self.name = name
self.age = age
self.course = course
def displayPerson(self):
print("Person: " + self.name + ", " + str(self.age) + ", " + self.course)
Additional Python Resources
https://www.halvorsen.blog/documents/programming/python/
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: [email protected]
Web: https://www.halvorsen.blog