Python L07 ClassObj
Python L07 ClassObj
Python
OBJECTIVES:
2
Class and object
Object instance
0973427
John Smith
3
Class and object
Class Staff
Staff number
Employment type (Full time/Part time/Casual)
First name
Last name
Date of birth
...
Class TV_Program
Channel name
Program title
Start time
End time
Category
... 4
Class and object
Staff Object 1
TV_Program Object 1
TV_Program Object 2
7
Instance attribute vs Class attribute
● Each student object has its own first name, last name and student id,
etc...
● Each staff object has its own staff id, date of birth, employment type,
etc…
● Each TV program object has its own channel name, program title, start
time, end time, etc...
8
Instance attribute vs Class attribute
class Student:
#{
email_domain = "solla.sollew.edu"
student_dir = "/user/student" class attributes
#}
For example,
10
Instance attribute vs Class attribute
class Student:
#{
email_domain = "solla.sollew.edu"
student_dir = "/user/student" class attributes
#}
11
Instance method vs Class/Static method
Instance method:
● Deal with a particular individual object instance
12
Instance method vs Class/Static method
Instance method:
● Deal with particular individual object instance
For example:
● Get the full name of a Student object
Instance method:
● Deal with individual object instance attributes
● The first argument (self) is always referred to the
object instance
class Student:
#{
def fullname(self)...
def print_detail(self)...
#}
14
Instance method vs Class/Static method
Instance method:
● Deal with individual object instance attributes
● The first argument (self) is always referred to the
object instance
class TV_Program:
#{
def get_length_in_minutes(self)...
#}
15
Instance method vs Class/Static method
Instance method:
● instance method can be invoked from an object
staff2.update_employment_type("Casual")
staff3.update_employment_type("Fulltime")
length2 = tv_program2.get_length_in_minutes()
length5 = tv_program5.get_length_in_minutes()
minute_count5 = tv_program5.time_left_in_minutes(now)
minute_count3 = tv_program3.time_left_in_minutes(now)
16
Instance method vs Class/Static method
For example:
● Get the shared student email domain (solla.sollew.edu)
contact_email = Student.admin_email()
url = Student.uni_website()
studentObj = Student.find_by_student_id("0783122")
tv_program_list1 = TV_Program.find_by_time(now)
18
Instance method vs Class/Static method
@classmethod
def admin_email(cls):
return "admin@" + cls.email_domain
@staticmethod
def uni_website():
return "http://www.solla.sollew.edu"
#}
19
Defining class and creating object
class Fish:
#{
#}
20
Defining class and creating object
class Student:
#{
#}
class Fish:
def
init
(self,
name,
# color,
creating fish objects
address):
shark = Fish("Bruce", "gray", "Sydney aquarium")
... = Fish("Goldie", "orange", "Darling River")
goldfish
angelfish = Fish("Finley", "blue", "Joe’s fish tank")
22
Accessing object instance attributes
class Student:
23
Modify object instance attributes
angelfish = Fish("Finley", "blue", "Joe’s fish tank")
Before: After:
Finley Finley
blue blue
Joe’s Uni 25
Modify object instance attributes
student2 = Student("1882845", "Mary", "Wilson")
Before: After:
1882845 1882845
Mary Mary
Wilson Davis 26
Defining an object instance method
class Student:
#{
. . .
def fullname(self):
#{
#}
#}
Instance method:
● Automatically pass the object instance (self) as the
first parameter
26
Defining an object instance method
class Student:
def fullname(self):
#{
#}
27
Documenting Python code
class Student:
#{
"""
Class Student represents a student
"""
def fullname(self):
#{
"""
Get student's full name
"""
return self.first_name
+ " " + self.last_name
#}
#}
It is important to write documentation of your class and methods.
This helps users to understand the usage and functionality of your code.
help(Student) 29
Help method
print(help(Student))
class Student(builtins.object)
| Class Student represents a student
| with the following attributes:
| id: student number
| first_name: first name
| last_name: last name
| username: Unix account username
|
| Methods defined here:
|
| init (self, id, first_name, last_name)
| Initialize self. See help(type(self)) for accurate signature.
|
| repr (self)
| Return repr(self).
|
| str (self)
| Return str(self).
|
| email(self)
| Get student's email: username@domain
|
| email_alias(self)
| Get student's friendly-looking email:
| firstname.lastname.3IDdigits@domain
|
| fullname(self)
| Get student's full name
|
| home_dir(self)
| Get student's Unix home directory: studentDir/username
|
| print_detail(self)
| Display student detail
|
|
| Data descriptors defined here:
|
| dict
| dictionary for instance variables (if defined)
|
| weakref
| list of weak references to the object (if defined)
|
|
| Data and other attributes defined here:
|
| email_domain = 'solla.sollew.edu'
|
| student_dir = '/user/student' 30
Case study example:
class Student:
"""
Class Student represents a student
with the following attributes:
id: student number
first_name: first name
last_name: last name
username: Unix account username
"""
email_domain = "solla.sollew.edu"
student_dir = "/user/student" class attributes
31
Defining class Student
class Student:
def init (self, id, first_name, last_name):
self.id = id
self.first_name = first_name
self.last_name = last_name
object attributes
# username is constructed in the beginning
# and will not change if name changed
# username = lowercase initials + first 3 id digits
self.username = first_name[0].lower() + last_name[0].lower() + id[0:3]
32
Creating Student objects
class Student:
33
Accessing object instance attributes
class Student:
34
Accessing class attributes
class Student:
email_domain = "solla.sollew.edu"
student_dir = "/user/student"
35
Modify object instance attributes
student2 = Student("1882845", "Mary", "Wilson")
Before: After:
1882845 1882845
Mary Mary
Wilson Davis 37
Modify class attributes
37
Defining an object instance method
class Student:
def fullname(self):
"""
Get student's full name
"""
return self.first_name +
" " + self.last_name
Instance method:
● Automatically pass the object instance (self) as the
first parameter
● May use instance attribute and instance method
38
Defining an object instance method
class Student:
def fullname(self):
"""
Get student's full name
"""
return self.first_name +
" " + self.last_name
39
Defining an object instance method
class Student:
def fullname(self):
return self.first_name + " " + self.last_name
class Student:
def email(self):
"""
Get student's email: username@domain
"""
return self.username + "@" +
Student.email_domain
# display email
print(student2.email())
41
Defining an object instance method
class Student:
def email_alias(self):
"""
Get student's friendly-looking email:
firstname.lastname.3IDdigits@domain
"""
return self.first_name + "." + self.last_name + "." +
self.id[0:3] + "@" + Student.email_domain
42
Defining an object instance method
class Student:
def home_dir(self):
"""
Get student's Unix home directory:
studentDir/username
"""
return Student.student_dir + "/" +
self.username
/user/student/mw188
43
Defining an object instance method
class Student:
def print_detail(self):
print("Student ID: " + self.id)
print("First name: " + self.first_name)
print("Last name: " + self.last_name)
print("Full name: " + self.fullname())
print("Username: " + self.username)
print("Email: " + self.email())
print("Email alias: " + self.email_alias())
print("Home directory: " + self.home_dir())
# display details
student2.print_detail()
44
Defining an object instance method
print("Before:")
student2.print_detail()
print("After:")
student2.print_detail()
Before:
Student ID: 1882845
First name: Mary
Last name: Wilson
Full name: Mary Wilson
Username: mw188
Email:
[email protected]
Email alias:
Home directory: /user/student/mw188 45
Defining an object instance method
print("Before:")
student2.print_detail()
print("After:")
student2.print_detail()
After:
Student ID: 1882845
First name: Mary
Last name: Davis
Full name: Mary Davis
Username: mw188
Email:
[email protected]
u
Home directory: /user/student/mw188 46
Thank you!