super
Functionisinstance
FunctionI have posted homework 11 here.
It is due this coming Sunday at 11:59 PM.
This is the last homework assignment.
from video import Video
class SUBCLASS_NAME(SUPERCLASS_NAME):
class Movie(Video):
Video.__init__(...)
def __init__(self, name, length, format, director, studio): Video.__init__(self, name, length, format) self.__director = director self.__studio = studio self.__actors = []
>>> from movie import Movie >>> m1 = Movie('Forbidden Planet', 98, 'DVD', 'Fred McLeod Wilcox', 'MGM') >>> str(m1) '1: Forbidden Planet, 98 minutes, DVD'
>>> m1.get_collection_no() 1 >>> m1.get_name() 'Forbidden Planet' >>> m1.get_length() 98 >>> m1.get_format() 'DVD'
def get_director(self): return self.__director def get_studio(self): return self.__studio def get_actors(self): return self.__actors
>>> m1.get_director() 'Fred McLeod Wilcox' >>> m1.get_studio() 'MGM' >>> m1.get_actors() []
>>> m1.add_actor('Walter Pidgeon') >>> m1.add_actor('Anne Francis') >>> m1.add_actor('Leslie Nielson') >>> m1.add_actor('Warren Stevens') >>> m1.get_actors() ['Walter Pidgeon', 'Anne Francis', 'Leslie Nielson', 'Warren Stevens']
def __str__(self): return str(self.__collection_no) + ': ' + self.__name + ', ' + \ str(self.__length) + ' minutes, ' + self.__format + \ ' Directed by ' + self.__director + ', ' + self.__studio
>>> str(m1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/glenn/workspace-mars/it117/resources_it117/code_it117/example_code_it117/11_chapter_example_code/movie.py", line 26, in __str__
' Directed by ' + self.__director + ', ' + self.__studio
AttributeError: 'Movie' object has no attribute '_Movie__collection_no'
super
Functionsuper
super
returns something like an object ...super
... def __str__(self):
return super().__str__() + ' Directed by ' + self.__director + ', ' + self.__studio
>>> str(m1) '9: Forbidden Planet, 98 minutes, DVD Directed by Fred McLeod Wilcox, MGM'
def __init__(self, course_name, company, disc_number, instructor, length, format): Video.__init__(self, 'XXX', length, format) self.__course_name = course_name self.__company = company self.__disc_number = disc_number self.__instructor = instructor self.__lectures = []
super().__str__()as we did in Movie ...
def __str__(self): return str(super().get_collection_no()) + ': ' + self.__course_name + ': Disc ' + \ str(self.__disc_number) + 'Instructor ' + self.__instructor
>>> from instructional import Instructional >>> i1 = Instructional('Understanding the Universe', 'Great Courses', 1, 'Alex Filippenko', 180, 'DVD') >>> str(i1) '21: Understanding the Universe: Disc 1, Instructor Alex Filippenko'
def get_name(self): return str(super().get_collection_no()) + ': ' + self.__course_name + ': Disc ' + \ str(self.__disc_number)
>>> i1.get_name() '21: Understanding the Universe: Disc 1'
def __str__(self): return self.get_name() + ', Instructor ' + self.__instructor
def add_lecture(self, lecture): self.__lectures.append(lecture)
def get_name(self): return str(super().get_collection_no()) + ': ' + self.__course_name + ': Disc ' + \ str(self.__disc_number) def get_course_name(self): return self.__course_name def get_company(self): return self.__company def get_disc_number(self): return self.__disc_number def get_instructor(self): return self.__instructor def get_lectures(self): return self.__lectures
>>> i1.add_lecture('A Grand Tour of the Cosmos') >>> i1.add_lecture('The Rainbow Connection') >>> i1.add_lecture('Sunrise, Sunset') >>> i1.add_lecture('Bright Objects in the Night Sky') >>> i1.add_lecture('Fainter Phenomena in the Night Sky') >>> i1.add_lecture('Our Sky Through Binoculars and Telescopes') >>> i1.get_course_name() 'Understanding the Universe' >>> i1.get_company() 'Great Courses' >>> i1.get_disc_number() 1 >>> i1.get_instructor() 'Alex Filippenko' >>> i1.get_lectures() ['A Grand Tour of the Cosmos', 'The Rainbow Connection', 'Sunrise, Sunset', 'Bright Objects in the Night Sky', 'Fainter Phenomena in the Night Sky', 'Our Sky Through Binoculars and Telescopes'
isinstance
Functionisinstance
built-in functionisinstance(OBJECT_VARIABLE, CLASS_NAME)
isinstance
is a boolean function ...>>> isinstance(i1, Movie) False >>> isinstance(i1, Instructional) True
isinstance
function will also return True ...>>> isinstance(i1, Video) True
$ cat function_calling_function.py #! /usr/bin/python3 # Demonstrates one function calling another def function_1(arg): print("This is function_1 printing it's argument:", arg) print() print('function_1 is now calling function_2("' + arg +'")') function_2(arg) print() print('This is function_1 signing off') def function_2(arg): print() print("This is function_2 printing it's argument:", arg) function_1('Hello') $ ./function_calling_function.py This is function_1 printing it's argument: Hello function_1 is now calling function_2("Hello") This is function_2 printing it's argument: Hello This is function_1 signing off
def add_one_and_print(num): num +=1 print(num) add_one_and_print(num)
1 2 3 4 5 ... 994 995 996 997 998
Traceback (most recent call last): File "./count_forever.py", line 7, in <module> add_one_and_print(0) File "./count_forever.py", line 5, in add_one_and_print add_one_and_print(num) File "./count_forever.py", line 5, in add_one_and_print add_one_and_print(num) File "./count_forever.py", line 5, in add_one_and_print add_one_and_print(num) File "./count_forever.py", line 5, in add_one_and_print
RecursionError: maximum recursion depth exceeded while getting the str of an object
def count_down(num): print(num) if num > 1: count_down(num - 1)
10 9 8 7 6 5 4 3 2 1
2! = 1 * 2 = 2
3! = 1 * 2 * 3 = 6 4! = 1 * 2 * 3 * 4 = 24 5! = 1 * 2 * 3 * 4 * 5 = 120
3! = 2! * 3 4! = 3! * 4 5! = 4! * 5
n! = (n-1)! * n
1! = 1
def factorial(num): if num == 1: return 1 else: return factorial(num -1) * num
>>> print("5!:",factorial(5)) 5!: 120
1 1 2 3 5 8 13 21 34 55
F(n) = F(n - 1) + F(n - 2)
F(1) = 1 F(2) = 1
def fibonacci(num): if num == 1 or num == 2: return 1 else: return fibonacci(num - 1) + fibonacci(num - 2)
for
loop
for n in range(1, 11): print(fibonacci(n), end=" ')
1 1 2 3 5 8 13 21 34 55
add the start element to the sum of the rest of the list
stop when the start index is greater than the end index
def range_sum(num_list, start, end): if start > end: return 0 else: return num_list[start] + range_sum(num_list, start + 1, end)
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print('list:', list) print('range_sum(list, 0, 0):',range_sum(list, 0, 0)) print('range_sum(list, 0, 1):',range_sum(list, 0, 1)) print('range_sum(list, 0, 2):',range_sum(list, 0, 2)) print('range_sum(list, 0, 3):',range_sum(list, 0, 3)) print('range_sum(list, 0, 4):',range_sum(list, 0, 4)) print('range_sum(list, 7, 9):',range_sum(list, 7, 9))
list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] range_sum(list, 0, 0): 1 range_sum(list, 0, 1): 3 range_sum(list, 0, 2): 6 range_sum(list, 0, 3): 10 range_sum(list, 0, 4): 15 range_sum(list, 7, 9): 27
range
def range_sum(num_list, start, end): sum = 0 for index in range(start, end +1): sum += list[index] return sum
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print('list:', list) print('range_sum(list, 0, 0):',range_sum(list, 0, 0)) print('range_sum(list, 0, 1):',range_sum(list, 0, 1)) print('range_sum(list, 0, 2):',range_sum(list, 0, 2)) print('range_sum(list, 0, 3):',range_sum(list, 0, 3)) print('range_sum(list, 0, 4):',range_sum(list, 0, 4)) print('range_sum(list, 7, 9):',range_sum(list, 7, 9))
list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] range_sum(list, 0, 0): 1 range_sum(list, 0, 1): 3 range_sum(list, 0, 2): 6 range_sum(list, 0, 3): 10 range_sum(list, 0, 4): 15 range_sum(list, 7, 9): 27
set a factorial variable to 1
set a count variable to 1
while count is less than the number whose factorial we are computing
increment count
set factorial to count times the current value of factorial
return the factorial value
def factorial(number): value = 1 count = 1 while count < number: count += 1 value *= count return value
>>> print("5!:",factorial(5)) 5!: 120
if the number is 1
return 1
else:
set the first number variable to 0
set the second number variable to 1
set count to 1
while count is less than the number
increment count by 1
set value to first number plus the second number
set first to second
set second to value
return value
def fibonacci(number): if number == 1: return 1 else: first = 0 second = 1 count = 1 while count < number: count += 1 value = first + second first = second second = value return value
def fibonacci(num): if num == 1 or num == 2: return 1 else: return fibonacci(num - 1) + fibonacci(num - 2)