Are there any questions before I begin?
There is no graded quiz today, but there will be one next Tuesday
I have posted a solution to homework 8 here.
class CLASS_NAME(Object):
def __init__(self, id, last_name, first_name):
self.__id = id
self.__last_name = last_name
self.__first_name = first_name
self.__preferred_name = ""
self.__phonetic_name = ""
self.__email = ""
self.__unix_username = ""
def get_id(self):
return self.__id
def get_last_name(self):
return self.__last_name
def get_first_name(self):
return self.__first_name
def get_preferred_name(self):
return self.__preferred_name
def get_phonetic_name(self):
return self.__phonetic_name
def get_email(self):
return self.__email
def get_unix_username(self):
return self.__unix_username
def set_preferred_name(self, preferred_name):
self.__preferred_name = preferred_name
def set_phonetic_name(self, phonetic_name):
self.__phonetic_name = phonetic_name
def set_email(self, email):
self.__email = email
def set_unix_username(self, unix_username):
self.__unix_username = unix_username
def __str__(self):
return self.__first_name + " " + self.__last_name + ", " + self.__id
def get_full_name(self):
if self.__preferred_name:
return self.__preferred_name + " " + self.__last_name
else:
return self.__first_name + " " + self.__last_name
def get_sort_name(self):
if self.__preferred_name:
return self.__last_name + ", " + self.__preferred_name
else:
return self.__last_name + ", " + self.__first_name
import pickle
pickle_file = open(pickle_filename, "wb")
pickle.dump(students, pickle_file)
pickle_file = open(pickle_filename, "rb")
students = pickle.load(pickle_file)
import statements at the top of the file ...from student import Student import pickle class Section: ...
x = 5
x += 7 y = x + 8 x > y
diff = t1.difference(t2)
diff = t1 - t2
| Operator | Magic Method |
|---|---|
| + | __add__(self, other) |
| - | __sub__(self, other) |
| * | __mul__(self, other) |
| // | __floordiv__(self, other) |
| / | __truediv__(self, other) |
| % | __mod__(self, other) |
| ** | __pow__(self, other) |
# returns the difference in seconds between two times
def __sub__(self, other_time):
return self.__seconds - other_time.__seconds
>>> t1 = Time("10:45:10")
>>> t2 = Time("10:50:00")
>>> t2 - t1
290
>>> t2 - t1
| Operator | Magic Method |
|---|---|
| == | __eq__(self, other) |
| != | __ne__(self, other) |
| < | __lt__(self, other) |
| > | __gt__(self, other) |
| <= | __le__(self, other) |
| >= | __ge__(self, other) |
def __eq__(self, other):
return self.__seconds == other.__seconds
def __ne__(self, other):
return self.__seconds != other.__seconds
def __lt__(self, other):
return self.__seconds < other.__seconds
def __gt__(self, other):
return self.__seconds > other.__seconds
def __le__(self, other):
return self.__seconds <= other.__seconds
def __ge__(self,other):
return self.__seconds >= other.__seconds
>>> t1 = Time("10:45:10")
>>> t2 = Time("10:50:00")
>>> t3 = Time("10:50:00")
>>> t1 == t2
False
t2 == t3
True
>>> t1 < t2
True
>>> t1 > t2
False
>>> t1 <= t2
True
>>> t2 <= t3
True
>>> t1 >= t2
False
>>> t2 >= t3
True
intfloatboolstr| Operator | Magic Method |
|---|---|
int |
__int__(self) |
float |
__float__(self) |
bool |
__bool__(self) |
str |
__str__(self) |
def __int__(self):
return self.__seconds
def __float__(self):
return self.__seconds * 1.0
True
>>> bool(0) False >>> bool(5) True >>> bool(-5) True
def __bool__(self):
return self.__seconds != 0
>>> t1 = Time("10:15:00")
>>> t2 = Time("00:00:00")
>>> str(t1)
'10:15:0 AM'
>>> bool(t1)
True
>>> bool(t2)
False
>>> int(t1)
36900
>>> int(t2)
0
>>> float(t1)
36900.0
def __eq__(self, other):
return self.__seconds == other.__seconds
def __eq__(self, other):
return self.__manufacturer == other.get_manufacturer() and self.__model == other.get_model()
def __ne__(self, other):
return self.__manufacturer != other.get_manufacturer() or self.__model != other.get_model()
c1 = Car("Honda", "CRV", "1997", "Blue")
c2 = Car("Honda", "CRV", "2010", "Green")
c3 = Car("Honda", "Civic", "2015", "Red")
print(c1)
print(c2)
print(c3)
print("c1 == c2 :" , c1 == c2 )
print("c1 == c3 :" , c1 == c3 )
print("c1 != c3 :" , c1 != c3 )
Honda CRV 1997 Blue Honda CRV 2010 Green Honda Civic 2015 Red c1 == c2 : True c1 == c3 : False c1 != c3 : True
>>> from car4 import Car Honda CRV 1997 Blue Honda CRV 2010 Green Honda Civic 2015 Red c1 == c2 : True c1 == c3 : False c1 != c3 : True
if statement
if __name__ == "__main__":
c1 = Car("Honda", "CRV", "1997", "Blue")
c2 = Car("Honda", "CRV", "2010", "Green")
c3 = Car("Honda", "Civic", "2015", "Red")
print(c1)
print(c2)
print(c3)
print("c1 == c2 :" , c1 == c2 )
print("c1 == c3 :" , c1 == c3 )
print("c1 != c3 :" , c1 != c3 )
$ ./car3.py Honda CRV 1997 Blue Honda CRV 2010 Green Honda Civic 2015 Red c1 == c2 : True c1 == c3 : False c1 != c3 : True
>>> from car3 import Car >>>
ii
def __init__(self, real, imaginary): self.__real = int(real) self.__imag = int(imaginary)
5 + 3iwhere
i is the square root of minus 1
def __str__(self): return str(self.__real) + " + " + str(self.__imag) + "i"
def __add__(self, other): return Complex(self.__real + other.__real, self.__imag + other.__imag)
def __sub__(self, other): return Complex(self.__real - other.__real, self.__imag - other.__imag)
def __mul__(self, other): new_real = self.__real * other.__real - self.__imag * other.__imag new_imag = self.__real * other.__imag + self.__imag * other.__real return Complex(new_real, new_imag)
if __name__ == "__main__":
c1 = Complex(3,2)
c2 = Complex(1,7)
print("c1:", c1)
print("c2:", c2)
print("c1 + c2:", c1 + c2)
print("c1 - c2:", c1 - c2)
print("c1 * c2:", c1 * c2)
$ python3 complex.py c1: 3 + 2i c2: 1 + 7i c1 + c2: 4 + 9i c1 - c2: 2 + -5i c1 * c2: -11 + 23i