OOP Asynchronous Functions
Object-Oriented Programming (OOP) is a programming paradigm in computer science that relies on the concept of classes and objects.
Introduction To Object Oriented Programming
Object Oriented Programming
Object-Oriented Programming (OOP) is a programming paradigm in computer science that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects.
Building blocks of OOP
- Classes - a blueprint of an object
- Objects - an instance of a class
- Methods - methods represent behaviors
- Attributes - information to be stored in a class about an object
Four Principles of OOP
The four pillars of object-oriented programming are:
- Inheritance: child classes inherit data and behaviors from the parent class
- Encapsulation: containing information in an object, exposing only selected information
- Abstraction: only exposing high-level public methods for accessing an object
- Polymorphism: many methods can do the same task
What are the benefits of OOP?
- Modularity
Encapsulation enables objects to be self-contained, making troubleshooting and collaborative development easier.
- Reusability
Code can be reused through inheritance, meaning a team does not have to write the same code multiple times.
- Productivity
Programmers can construct new programs quickly through the use of multiple libraries and reusable code.
- Easily upgradable and scalable
Programmers can implement system functionalities independently. Interface descriptions
Descriptions of external systems are simple, due to message-passing techniques that are used for object communication.
- Security
- Using encapsulation and abstraction, complex code is hidden, software maintenance is easier and internet protocols are protected.
- Flexibility
- Polymorphism enables a single function to adapt to the class it is placed in. Different objects can also pass through the same interface.
Python Classes/Objects
Python is a completely object-oriented language. You have been working with classes and objects right from the beginning of this course.
Every element in a Python program is an object of a class, with its properties and methods. A Class is like an object constructor or a “blueprint” for creating objects.
A number, string, list, dictionary, etc., used in a program is an object of a corresponding built-in class. You can retrieve the class name of variables or objects using the type() method,
1
2
3
4
5
6
7
8
9
10
11
>>> num=20
>>> type(num)
<class 'int'>
>>> s="Python"
>>> type(s)
<class 'str'>
Create a Class
A class in Python can be defined using the class keyword.
Syntax
:
1
2
3
4
5
6
7
8
9
10
11
12
class <ClassName>:
<statement1>
<statement2>
.
.
<statementN>
As per the syntax above, a class is defined using the class
keyword followed by the class name and: operator after the class name, which allows you to continue in the next indented line to define class members.
The following are class members:
Class attributes Constructor Instance attributes Properties Class methods
A class can also be defined without any members. The following example defines an empty class using the pass
keyword.
1
2
3
4
class Person:
pass
We will discuss the class members in detail in the next lesson
Class Attributes, Constructors, Instance Attricbutes and Class Methods Explained
Class Attributes
Class attributes are the variables defined directly in the class that are shared by all objects of the class. Class attributes can be accessed using the class name as well as using the objects.
1
2
3
class Person:
name = 'Skinny’ #Class attribute
Above, the name is a class attribute defined inside a class Person. The value of the name will remain the same for all the objects unless modified explicitly.
1
2
3
4
5
6
7
8
9
10
11
Accessing class attributes
>>> Person.name
'Skinny'
>>> details = Person()
>>> details.name
'Skinny'
Constructor
In Python, the constructor method is invoked automatically whenever a new object of a class is instantiated, same as constructors in C++ or Java.
The constructor must have a special name init() and a special parameter called self.
All classes have a function called init(), which is always executed when the class is being initiated.
The constructor in Python is used to define the attributes of an instance and assign values to them.
NB: The first parameter of each method in a class must be the self, which refers to the calling object. However, you can give any name to the first parameter, not necessarily self.
Example of how to define a constructor:
1
2
3
4
5
6
class Person:
def __init__(self): # constructor method
print('Constructor invoked')
Now, whenever you create an object of the Person class, the init() constructor method will be called, as shown below.
1
2
3
4
5
6
7
>>>details1 = Person()
Constructor invoked
>>>details2 = Person()
Constructor invoked
Instance Attributes
Instance attributes are attributes or properties attached to an instance of a class. Instance attributes are defined in the constructor.
The following example defines instance attributes name and age in the constructor.
1
2
3
4
5
6
7
8
9
10
class Person:
nationality = 'Ethiopian' # class attribute
def __init__(self): # constructor
self.name = '' # instance attribute
self.age = 0 # instance attribute
To access an instance variable, we use dot notation:
[instance name].[attribute name], as shown below
1
2
3
4
5
6
7
8
9
>>> p1 = Person()
>>> p1.name
''
>>> p1.age
0
You can set the value of attributes using the dot notation, as shown below.
1
2
3
4
5
6
7
8
9
10
11
12
13
>>> p1 = Person()
>>> p1.name = "Mutemi" # assign value to instance attribute
>>> p1.age = 65 # assign value to instance attribute
>>> p1.name # access instance attribute value
Mutemi
>>> std.age # access value to instance attribute
65
The best practice is to always specify the values of instance attributes through the constructor.
Setting Attribute Values
The following constructor includes the name and age parameters, other than the self parameter.
class Person:
1
2
3
4
5
6
7
# name & age parameters passed in constructor
def __init__(self, name, age):
self.name = name
self.age = age
Passing Instance Attribute Values in Constructor
Now, you can specify the values while creating an instance, as shown below.
1
2
3
4
5
6
7
8
9
>>> p1 = Person('Mutemi', 65)
>>> p1.name
'Mutemi'
>>> p1.age
65
Setting Default Values of Instance Attributes
Also, you can set default values to instance attributes. By doing this, if the values are not provided when creating an object, the default values will be assigned.
Lets assign name=”mkuu” and age=101
1
2
3
4
5
6
7
8
class Person:
def __init__(self, name="mkuu", age=101)
self.name=name
self.age=age
Instance Attribute Default Value
Now, you can create an object with default values, as shown below
1
2
3
4
5
6
7
8
9
>>> p1 = Person()
>>> p1.name
'Guest'
>>> p1.age
65
Class Methods
A python function in a class is called class method. Methods are defined using the def keyword. Each method must have a self as the first parameter, which refers to calling the instance.
Self is just a conventional name for the first argument of a method in the class. A method defined as mymethod(self, a, b) should be called as x.mymethod(a, b) for the object x of the class.
Example: here we have a method named displayInfo
1
2
3
4
5
class Person:
def displayInfo(self): # class method
print('Personal Information')
The above class method can be called as a normal function, as shown below.
1
2
3
4
5
6
>>> p1 = Person()
>>> p1.displayInfo()
'Personal Information'
Let’s combine our knowledge so far for class constructors and methods to access instance attributes using self parameter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Person:
def __init__(self, name, age): # class constructor
self.name = name
self.age = age
def displayInfo(self): # class method
print('Person Name: ', self.name,', Age: ', self.age)
Calling a Method
Let’s call/Invoke the displayInfo method as shown below:
1
2
3
4
5
6
>>> p1 = Person('Mutemi', 65)
>>> p1.displayInfo()
Person Name: Mutemi , Age: 65
File Handling in Python
Understanding how to read and write files
Input and Output in Python refer to the process of taking input from the user and displaying output to the user. The input function is used to take input from the user, and the print function is used to display output to the user.
Here's a simple example to illustrate this:
1
2
name = input("Enter your name: ")
print("Hello, World")
In this example, the input function takes a string argument (prompt) which is displayed to the user, and it returns the user’s input as a string. The input function is used to take input from the user and store it in a variable. The print function is used to display output to the user by printing its argument(s).
Another explanation is that
Input and Output in Python are two important concepts that are used to interact with the user.
Input refers to the process of taking data from the user and storing it in a program for further processing. This can be done using the input function in Python. The input function takes a string argument (prompt) which is displayed to the user, and it returns the user’s input as a string.
1
2
name = input("What is your name? ")
age = input("What is your age? ")
In the above code, the input function is used to prompt the user to enter their name and age. The input data is then stored in the variables’ names and ages, respectively.
The print function, on the other hand, is used to display output to the user by printing its argument(s) to the screen. The argument(s) can be a string, variable, or expression. For example:
1
2
print("Hello, "+name+ "! You are "+age+" years old")
Output refers to the process of displaying the result of a computation or a processing step to the user. This can be done using the print function in Python. The print function is used to display output to the user by printing its argument(s).
1
2
result = 2 + 3
print("The result is:", result)
Output:
The result is: 5
In addition to the input and print functions, there are other ways to handle Input-Output in Python, such as reading and writing to files, using command line arguments, or using third-party libraries.
Why do we use Input and Output in Python:
Input and Output are used in Python to allow the user to interact with the program. The use of Input and Output enables the program to receive data from the user and provide results to the user.
Here are some common use cases for Input and Output in Python:
- Taking input from the user: The input function is used to take input from the user, such as a name, age, or any other type of data. The input received from the user can be stored in a variable for further processing.
- Displaying output to the user: The print function is used to display output to the user. This can be used to display the result of a calculation, the contents of a list or dictionary, or any other type of data.
- Reading data from a file: Input can also be taken from a file, such as a text file, CSV file, or any other type of file. This can be done using the open function in Python or using third-party libraries like Pandas.
- Writing data to a file: Output can also be stored in a file, such as a text file, CSV file, or any other type of file. This can be done using the open function in Python or using third-party libraries like Pandas.
Input and Output are essential in making the program more interactive and user-friendly. They also allow the program to receive and provide data, which is crucial for many applications.
Python (Files I/O)
Understanding how to read and write files:
In Python, reading from and writing to files can be accomplished using the open function. The open function takes two arguments: the name of the file and the mode in which the file should be opened.
There are four modes in which a file can be opened in Python:
- “r” (Read Only): This mode is used to only read the contents of a file. If the file does not exist, a FileNotFoundError will be raised.
- “w” (Write Only): This mode is used to write to a file. If the file already exists, its contents will be truncated (i.e., deleted), and a new file with the same name will be created. If the file does not exist, a new file with the specified name will be created.
- “a” (Append Only): This mode is used to append to an existing file. If the file does not exist, a new file with the specified name will be created.
- “x” (Write Only, Exclusive Creation): This mode is used to write to a file only if the file does not already exist. If the file already exists, a FileExistsError will be raised.
Here’s an example of how to read from a file in Python:
1
2
3
4
5
6
7
8
9
10
11
# Open file for reading
file = open("example.txt", "r")
# Read the content of the file
content = file.read()
#print the content of the file
print(content)
# Close the file
file.close()
And here's an example of how to write to a file in Python:
1
2
3
4
5
6
7
8
9
# Open file for writing
file = open("example.txt", "w")
#write some text to the file
file.write("Hello, world!\n")
file.write("This is a test.\n")
# Close the file
file.close()
It’s recommended to use the with the statement when opening files in Python, as it automatically takes care of closing the file when the code block is exited, even if an exception is raised
Reading Keyboard Input
Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. These functions are −
- raw_input
- input
The raw_input Function
The raw_input([prompt]) function reads one line from standard input and returns it as a string (removing the trailing newline).
Flat Files vs. Non-Flat Files
Flat files are data files that contain records with no structured relationships between the records, and there’s also no structure for indexing like you typically find it in relational databases. These files can contain only basic formatting, have a small fixed number of fields, and can or can not have a file format.
Python File Objects
Python has in-built functions to create, read, write, and manipulate accessible files. The io module is the default module for accessing files that can be used off the shelf without even importing it. Before you read, write, or manipulate the file, you need to make use of the module open(filename, access_mode) that returns a file object called “handle”. After which you can simply use this handle to read from or write to a file. Like everything else, files in Python are also treated as an object, which has its own attributes and methods.
An IOError exception is raised if, while opening the file, the operation fails. It could be due to various reasons like trying to read a file that is opened in write mode or accessing a file that is already closed.
As you already read before, there are two types of flat files, text and binary files:
As you might have expected from reading the previous section, text files have an End-Of-Line (EOL) character to indicate each line’s termination. In Python, the new line character (\n) is the default EOL terminator. Since binary files store data after converting it into a binary language (0s and 1s), there is no EOL character. This file type returns bytes. This file is used when dealing with non-text files such as images, .exe, or .pyc.
Let’s now understand the Python file objects in detail, along with necessary examples.
Open()
The built-in Python function open() has the following arguments: open(file, mode=’r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) The open() function has almost 8 parameters along with their default values for each argument as shown above.
You would be focusing on the first and second parameters for now, which are essential for reading and writing files. And go through other parameters one by one as the tutorial progresses.
Let’s understand the first argument, i.e., file.
File
file is a mandatory argument that you have to provide to the open function while the rest of the arguments are optional and use their default values.
To put it simply, the file argument represents the path where your file resides in your system.
If the path is in the current working directory, you can just provide the filename. If not then you have to provide the absolute path of the file, just like in the following examples: my_file_handle=open(“mynewtextfile.txt”) If the file resides in a directory other than the current directory, you have to provide the absolute path with the file name:
1
2
3
my_file_handle = open("mynewtestfile.txt")
my_file_handle = open("/path/to/mynewtextfile.txt") #different folder
my_file_handle = open("c:\\path\\to\\mynewtextfile.txt") #Windows machine you can use backslashes
Let’s understand the second argument of the open function, i.e., access modes.
Access Modes
Access modes define in which way you want to open a file, whether you want to open a file in:
- read-only mode
- write-only mode
- append mode
- both read and write mode
Though a lot of access modes exist as shown in the below table, the most commonly used ones are read and write modes. It specifies where you want to start reading or writing in the file.
You use ’r’, the default mode, to read the file. In other cases where you want to write or append, you use ’w’ or ’a’, respectively.
There are, of course, more access modes! Take a look at the following table:
As you have seen in the first section, there are two types of flat files. This is also why there’s an option to specify which format you want to open, such as text or binary. Of course, the former is the default. When you add ’b’ to the access modes, you can read the file in binary format rather than the default text format. It is used when the file to be accessed is not in text format.
Understanding the file methods (read, write, append, seek(), tell())
The open function returns a file object, which has several methods that can be used to manipulate the file. Some of the most commonly used methods are:
- read: This method is used to read the contents of a file. By default, the read method reads the entire contents of the file and returns it as a string. If a number is passed as an argument to the read method, it reads that many characters from the file and returns them as a string.
- write: This method is used to write to a file. The argument passed to the write method is written to the file as a string. If the file was opened in write mode (“w”), the contents of the file will be truncated and the new data will be written to the file. If the file was opened in append mode (“a”), the new data will be appended to the end of the file.
- seek: This method is used to change the file pointer’s position. The argument passed to the seek method is the number of bytes to move the file pointer. The seek method takes two optional arguments: the first argument is the number of bytes to move the file pointer, and the second argument is the starting point from where the file pointer should be moved. The starting point can be one of the following: 0 (the beginning of the file), 1 (the current position of the file pointer), or 2 (the end of the file).
- tell: This method is used to return the current position of
Working with Files: Basic Syntax
One of the most important functions that you will need to use as you work with files in Python is open(), a built-in function that opens a file and allows your program to use it and work with it.