0% found this document useful (0 votes)
51 views

Create Classes in Python

This document discusses object-oriented programming concepts in Python including creating classes, using classes, inheritance, and modules. It provides examples of defining a Car class with properties like model and color, and a display method. It also demonstrates creating multiple car objects, modifying properties, and using a Person parent class with Teacher and Student child classes. The examples illustrate key OOP concepts in Python like classes, objects, methods, inheritance, and modules.

Uploaded by

Alok Jhaldiyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Create Classes in Python

This document discusses object-oriented programming concepts in Python including creating classes, using classes, inheritance, and modules. It provides examples of defining a Car class with properties like model and color, and a display method. It also demonstrates creating multiple car objects, modifying properties, and using a Person parent class with Teacher and Student child classes. The examples illustrate key OOP concepts in Python like classes, objects, methods, inheritance, and modules.

Uploaded by

Alok Jhaldiyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

https://www.halvorsen.

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

Variable Explorer window

Code Editor window

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"

print(car.color + " " + car.model)


Classes -Example
Let's create the following class Car:
Python Code: model = "" Define the Class
color = ""

car1 = Car()

We can create and car1.model = "Volvo"


use multiple Objects car1.color = "Blue"
of the same Class print(car1.color + " " + car1.model)
Use the
Class
car2 = 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.

We will create a simple example class Car:


where we use the __init__() function def __init__(self, model, color):
to illustrate the principle: self.model = model
self.color = color

The self parameter is a reference car1 = Car("Ford", "Green")


to the current instance of the class print(car1.color + " " + car1.model)
and is used to access variables that
belongs to the class. car2 = Car("Volvo", "Blue")
print(car2.color + " " + car2.model)
The __init__() Function
# Defining the Class Car
Let's extend the Class by class Car:
defining a Function as def __init__(self, model, color):
well: self.model = model
self.color = color

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()

car3 = Car("Volvo", "Blue")


car3.displayCar()
Modifying a Property car3.color="Black"
car3.displayCar()
Classes - Summary
• As you see from the code, we have now defined a Class "Car"
that has 2 Class variables called "model" and "color” (these
variables are called Properties), and in addition we have defined
a Function (or Method) called "displayCar()".
• Its normal to use the term "Method" for Functions that are
defined within a Class.
• You declare class methods like normal functions with the
exception that the first argument to each method is self.
• To create instances of a class, you call the class using class name
and pass in whatever arguments its __init__() method accepts,
e.g., car1 = Car("Tesla", "Red")
Create Class in separate File
We start by creating the Class and then we save the code in "Car.py": Python Module
class Car:
def __init__(self, model, color):
self.model = model
self.color = color
from Car import Car
def displayCar(self):
car1 = Car("Tesla", "Red")
print("Car: " + self.model + "-" + self.color)
car1.displayCar()

car2 = Car("Ford", "Green")


Then we create a Python Script (testCar.py) car2.displayCar()
where we are using the Class:
car3 = Car("Volvo", "Blue")
car3.displayCar()
car3.color="Black"
car3.displayCar()
Inheritance
• Another important aspect of Object-oriented Programming is
Inheritance
• Inheritance allows us to define a class that inherits all the
methods and properties from another class. testPerson.py
class Person: Person.py from Person import *
def __init__(self, name, age):
self.name = name teacher1 = Teacher("Knut Nilsen", 44)
self.age = age teacher1.displayPerson()

def displayPerson(self): student1 = Student("Per Hamsun", 20)


print("Person: " + self.name + ", " + str(self.age)) student1.displayPerson()

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

You might also like