This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
forked from slgraff/edx-mitx-6.00.1x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec12.1-person.py
58 lines (48 loc) · 1.43 KB
/
lec12.1-person.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# lec12.1-person.py
#
# Lecture 12 - Object Oriented Programming
# Video 1 - Inheritance
#
# edX MITx 6.00.1x
# Introduction to Computer Science and Programming Using Python
import datetime
class Person(object):
def __init__(self, name):
"""create a person called name"""
self.name = name
self.birthday = None
self.lastName = name.split(' ')[-1]
def getLastName(self):
"""return self's last name"""
return self.lastName
def setBirthday(self,month,day,year):
"""sets self's birthday to birthDate"""
self.birthday = datetime.date(year,month,day)
def getAge(self):
"""returns self's current age in days"""
if self.birthday == None:
raise ValueError
return (datetime.date.today() - self.birthday).days
def __lt__(self, other):
"""return True if self's name is lexicographically
less than other's name, and False otherwise"""
if self.lastName == other.lastName:
return self.name < other.name
return self.lastName < other.lastName
def __str__(self):
"""return self's name"""
return self.name
me = Person("William Eric Grimson")
print me
foo = 'William Eric Grimson'
foo.split(' ')
foo.split(' ')[-1]
me.getLastName()
me.setBirthday(1,2,1927)
me.getAge()
her = Person("Cher")
her.getLastName()
plist = [me, her]
for p in plist: print p
plist.sort()
for p in plist: print p