In this example, the use of reflection and enumeration, there are a variety of considerations, is a good exercise
Total_couese_list = [] # Place variables in function run as global variables here Class Course (): Def __init__ (self, name, price, period): Self.name = Name Self.price = Price Self.period = Period Func_list = [{"Text": "Elective Course", "name": "Select_course", "par AM ":" CS "}, {" text ":" View Course "," name ":" Show_selectd_course "," param ":" None "}, {" text ":" Delete Course "," name ":" Del_selectd_course "," param ":" None "},]class Student (): Def __init__ (self, name): Self.name = Name Self.course = [] def select_course (self,): "" "Select Course, selected no longer" "" # When you are selected for class, list the chosen courses, with the following The enumeration in the main function is the same as for I, iterm in enumerate (total_couese_list, 1): # Enumerates the arguments, preceded by an iterator object, followed by the ordinal, and by default from a few starts print (I, iterm . Name,) # because an object is added to the list, the object can only be evaluated by the ". Property" Method num = input ("Please enter the course you want to choose") num = num-1 Course_obj = Tota L_couese_list[num] If course_obj not in Self.course:self.course.append (course_obj) def show_selectd_ Course (self): "" "View selected Courses "" "Def Del_selectd_course (self):" "" Delete selected Courses "" Def Run (): "" Main program 1, create 10 courses according to course class 2, user input name, create student Object 3 dynamically, view selected Course 4, select Course 5 for students, Delete selected courses: return: "" "# Total_couese_list=[] #把total_couese_list当成全局变量 for I in range: obj = Course (' xx-%s '% i, all, all) Total_couese_li St.append (obj) # added to the list is 10 instances Stu_name = input ("Please enter student Name:") # These two write together can be an object, which would have been nothing great, Stu = Student (stu_name) # but getting into a habit is a way of thinking for key, iterm in enumerate (course.func_list, 1): # Enumerates the arguments, preceded by an iterative object, followed by an ordinal, the default from several start print (key, iterm["Text"]) # Get course in the string, ready to do reflection while True: #***while This logical relationship is written in the fun, whil if shelf write the following instance Stu will not be recognized, in this fault, too pit n um = Int (Input ("Enter the function number to execute:") num = num-1 name = course.func_list[num]["Name"] # with hints from the enumeration, in relation to the user's input and enumeration To print (name) # Now the name value is taken to the function to be executed, just a string act1 = GetAttr (stu, name) # by reflection to the corresponding method in the student class, whichever method is the user Enter the ordinal, by enumerating the values associated to the string Print (Act1 ()) # above takes a function name # How do I pass a method in a class that is reflected to? The main reflection value when the parameter is a class or object, in is indentation, indentation #写错了, the logic is disorderly act1 () if __name__ = = "__main__": Run ()
The reflection of Python--an example of course selection