Lecture 1
Lecture 1
Problem Statement 2: Write a program employing the concept of OOP and its
pillars to build a simple GUI Staff Management System using Python (or any
other programming language of your choice!) which can perform the following
operations:
1.Accept new staff registration
2. Display staff data when required
3.Search for staff records
4.Delete staff record
5.Update student record
Method 1: Accept – This method takes details from the user like name,
matric- number, subjects offered, and other relevant student details.
Example:
Example:
Example:
Search Function
def search(self, a,b):
for i in range(ls.__len__()):
# iterate through the list containing
# student object and checks through
# name or mat no of each object
if(ls[i].matno == ….):
# returns the object with matching
# roll number
return i
Method 4: Delete – This method deletes the record of a particular student
or staff with a matching identifier that is defined.
Example:
# Delete Function
def delete(self, …..):
# Calls the search function
# created above
i = obj.search(….)
del ls[i]
Method 5: Update – This method updates the roll number of the student.
This method will ask for the old roll number and new roll number. It will
replace the old roll number with a new roll number.
Example:
# Update Function
def update(self, …, ….):
# calling the search function
# of student class
i = obj.search(…)
ls[i].matno = No
Procedural vs OOP
Until now, you have probably been coding in so-called procedural style: your code was a
sequence of steps to be carried out. This is common when doing data analysis: you download
the data, process, and visualize it.
Procedural thinking is natural. You get up, have breakfast, go to work. This sequential
viewpoint works great if you are trying to plan your day. But if you are a city planner, you have
to think about thousands of people with their own routines. Trying to map out a sequence of
actions for each individual would quickly get unsustainable. Instead, you are likely to start
thinking about patterns of behaviors. Same thing with code. The more data it uses, the more
functionality it has, the harder it is to think about as just a sequence of steps.
Instead, we view it as a collection of objects, and patterns of their interactions - like users
interacting with elements of an interface. This point of view becomes invaluable when
designing frameworks, like application program interfaces or graphical user interfaces, or
building tools like pandas DataFrames! It will help you organize your code better, making it
more reusable and maintainable.
Then a specific Customer object is just a realization of this class with particular state values
Objects in Python
In Python, everything is an object. Numbers, strings, DataFrames, even functions are objects.
In particular, everything you deal with in Python has a class, a blueprint associated with it
under the hood. The existence of these unified interfaces, is why you can use, for example, any
DataFrame in the same way. You can call type() on any Python object to find out its class. For
example, the class of a numpy array is actually called ndarray (for n-dimensional array).
Classes incorporate information about state and behavior. State information in Python is
contained in attributes, and behavior information -- in methods. Take a numpy array: you've
already been using some of its methods and attributes! For example, every numpy array has an
attribute "shape" that you can access by specifying the name of the array, then dot, and shape.
It also has methods, like max and reshape, which are also accessible via dot.
Object = attributes + methods
Attributes (or states) in Python objects are represented by variables -- like numbers, or strings,
or tuples, in the case of the numpy array shape. Methods, or behaviors, are represented by
functions. Both are accessible from an object using the dot syntax. You can list all the attributes
and methods that an object has by calling dir() on it. For example here, we see that a numpy
array has methods like trace and transpose.
Example:
We can define a class (Employee), and created an object of that class called mystery
What class does the mystery object have? Type(mystery)
So the mystery object is an Employee! You can further explore it in the console to find out what
attributes it has.
Help(mystery)
Help on Employee in module __main__ object:
class Employee(builtins.object)
| Employee(name, email=None, salary=None, rank=5)
|
| Class representing a company employee.
|
| Attributes
| ----------
| name : str
| Employee's name
| email : str, default None
| Employee's email
| salary : float, default None
| Employee's salary
| rank : int, default 5
| The rank of the employee in the company hierarchy (1 -- CEO, 2
-- direct reports of CEO, 3 -- direct reports of direct reports of CEO
etc). Cannot be None if the employee is current.
|
| Methods defined here:
|
| __init__(self, name, email=None, salary=None, rank=5)
| Create an Employee object
|
| give_raise(self, amount)
| Raise employee's salary by a certain `amount`. Can only be used
with current employees.
|
| Example usage:
| # emp is an Employee object
| emp.give_raise(1000)
|
| promote(self)
| Promote an employee to the next level of the company hierarchy.
Decreases the rank of the employee by 1. Can only be used on current
employeed who are not at the top of the hierarchy.
|
| Example usage:
| # emp is an Employee object
| emp.promote()
|
| terminate(self)
| Terminate the employee. Sets salary and rank to None..
|
| Example usage:
| # emp is an Employee object
| emp.terminate()
|
| -------------------------------------------------------------------
---
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
A basic class
To start a new class definition, all you need is a class statement, followed by the name
of the class, followed by a colon. Everything in the indented block after will be
considered a part of the class. You can create an "empty" class -- like a blank template
-- by including the pass statement after the class declaration. Even though this class
is empty, we can already create objects of the class by specifying the name of the
class, followed by parentheses. Here, c1 and c2 are two different objects of the empty
class Customer. We want to create objects that actually store data and operate on it -
- in other words have attributes and methods.
Add methods to a class
What is self?
So what was that self? Classes are templates. Objects of a class don't yet exist when
a class is being defined, but we often need a way to refer to the data of a particular
object within class definition. That is the purpose of self - it's a stand-in for the future
object. That's why every method should have the self argument -- so we could use it
to access attributes and call other methods from within the class definition even when
no objects were created yet. Python will handle self when the method is called from
an object using the dot syntax. In fact, using object-dot-method is equivalent to passing
that object as an argument. That's why we don't specify it explicitly when calling the
method from an existing object.
We need attributes
By the principles of OOP, the data describing the state of the object should be
bundled into the object. For example, customer name should be an attribute of a
customer object, instead of a parameter passed to a method. In Python attributes --
like variables -- are created by assignment, meaning an attribute manifests into
existence only when a value is assigned to it.
Here is a method set_name with arguments self (every method should have a self
argument) and new_name. To create an attribute of the Customer class called
"name", all we need to do is to assign something to self-dot-name. Remember, self
is a stand-in for object, so self-dot-attribute should remind you of the object-dot-
attribute syntax. Here, we set the name attribute to the new_name parameter of the
function. When we create a customer, it does not yet have a name attribute. But after
the set_name method was called, the name attribute is created, and we can access
it through dot-name.
Using attributes in class definition
Equipped with the name attribute, now we can improve our identification method!
Instead of passing name as a parameter, we will use the data already stored in the
name attribute of the customer class. We remove the name parameter from the
identify method, and replace it with self-dot-name in the printout, which, via self, will
pull the name attribute from the object that called the method. Now the identify
function will only use the data that is encapsulated in the object, instead of using
whatever we passed to it.
class Employee:
In this exercise, you'll go beyond the setter methods and learn how to use existing class
attributes to define new methods. The Employee class and the emp object from the previous
exercise are in your script pane.
class Employee:
def set_name(self, new_name):
self.name = new_name
emp = Employee()
emp.set_name('Korel Rossi')
emp.set_salary(50000)
print(emp.salary)
emp.give_raise(1500)
print(emp.salary)
class Employee:
def set_name(self, new_name):
self.name = new_name
emp = Employee()
emp.set_name('Korel Rossi')
emp.set_salary(50000)
# Print mon_sal
Good luck!