class Person(object):
def __init__(self, name):
#create a person with name name
self.name = name
try:
firstBlank = name.rindex(' ')
self.lastName = name[firstBlank+1:]
except:
self.lastName = name
self.age = None
def getLastName(self):
#return self's last name
return self.lastName
def setAge(self, age):
#assumes age is an int greater than 0
#sets self's age to age (in years)
self.age = age
def getAge(self):
#assumes that self's age has been set
#returns self's current age in years
if self.age == None:
raise ValueError
return self.age
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
class Resident(Person):
"""
A Person who resides in the US.
"""
def __init__(self, name, status):
"""
Initializes a Person object. A Resident object inherits
from Person and has one additional attribute:
status: a string, one of "citizen", "legal_resident", "illegal_resident"
Raises a ValueError if status is not one of those 3 strings
"""
Person.__init__(self, name)
if self.status in ['citizen', 'legal_resident', 'illegal_resident']:
pass
else:
raise ValueError
return self.status
def getStatus(self):
"""
Returns the status
"""
return self.status
a = Resident('Tim Beaver', 'citizen')
print a.getStatus()
b = Resident('Tim Horton', 'non-resident')
print b.getStatus()
def __init__(self, name):
#create a person with name name
self.name = name
try:
firstBlank = name.rindex(' ')
self.lastName = name[firstBlank+1:]
except:
self.lastName = name
self.age = None
def getLastName(self):
#return self's last name
return self.lastName
def setAge(self, age):
#assumes age is an int greater than 0
#sets self's age to age (in years)
self.age = age
def getAge(self):
#assumes that self's age has been set
#returns self's current age in years
if self.age == None:
raise ValueError
return self.age
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
class Resident(Person):
"""
A Person who resides in the US.
"""
def __init__(self, name, status):
"""
Initializes a Person object. A Resident object inherits
from Person and has one additional attribute:
status: a string, one of "citizen", "legal_resident", "illegal_resident"
Raises a ValueError if status is not one of those 3 strings
"""
Person.__init__(self, name)
if self.status in ['citizen', 'legal_resident', 'illegal_resident']:
pass
else:
raise ValueError
return self.status
def getStatus(self):
"""
Returns the status
"""
return self.status
a = Resident('Tim Beaver', 'citizen')
print a.getStatus()
b = Resident('Tim Horton', 'non-resident')
print b.getStatus()
![](http://e.hiphotos.bdimg.com/album/s%3D900%3Bq%3D90/sign=a2e035e6bb014a90853e4abd994c482f/8c1001e93901213f04f9c48856e736d12e2e9598.jpg?v=tbs)