原文地址: Closure漫談
經過一個實際場景,練習Python Closure的使用方法。python
The first part of your submission has to demonstrate the use of closures on a specific programming task using Python 3.5.app
The aim of the programming task is to model the basic functions of a project allocation system dealing with a number of supervisors and students. Each supervisor maintains a number of projects. Students can choose a project from the ones on offer. They can select a project and later change their mind and drop it or select another one, but each student can have at most one project allocated at a time. Your overall task will be to design, write and test procedures that model 'projects', 'students' and 'supervisors' as specified below.this
The program must be written in 'message-passing', or 'object-oriented' style as described in the POPL lectures, and Structure and Interpretation of Computer Programs (SICP). Your code should not make use of Python classes, and the keyword class must not appear in your code or its comments. Any line of code containing the keyword class will be removed from your solution before it is tested. It is acceptable though to make references to it in the essay, if needed.spa
The programming part of the assignment consists of a series of tasks, which you must complete in order. The transcript of a run of a valid solution for this part of the assignment can be found on the VLE under Assessment. The submitted code will be tested and marked in a fully automated way against a number of similar tests.code
Appendix A gives a skeleton procedure for constructing project objects, and three 'interface' procedures, project_supervisor, set_supervisor and projectID. Projects are constructed using the a_project(projectID) procedure, where projectID is a Python string (e.g., as constructed by "DLK1") representing the project's unique ID.1 You are to complete the implementation of this part of the programming task by filling in the missing code in the places indicated by ??? in the procedure a_project(...), and completing the definitions of the three interface procedures.orm
Any project object created with a_project(...) must respond to 'actions' as defined by the following interface procedures:blog
A student is an object that models students for the purpose of selecting a project. Design, implement and test a procedure a_student(studentID) that constructs a student object, where studentID is a Python string (e.g., as constructed by "abc234") representing the student's unique ID.three
An object constructed using a_student must maintain a record of the project selected (as constructed by a_project). Initially, a_student returns a student object with no project selected.ip
Objects constructed with a_student must respond to actions as defined by the following interface routines:ci
Design, implement and test a procedure a_supervisor(supervisorID) that models supervisors, where supevisorID is a Python string representing the supervisor's name. An object created by a_supervisor has the following properties:
Responds to actions as defined by the following interface procedures, in which supervisor is an object created by a_supervisor, student is an object created by a_student, and project is an object created by a_project:
This part of your submission needs to define what closures are, discuss some of the pros and cons of their use, compare the provisions for the use of closures made in Racket Scheme and Python 3.5, and, lastly, discuss the limitations of Python 2.7 for the implementation of the same programming task through closures as shown in Appendix A, and how these could be overcome.
The following aspects will be taken into account when marking the essay: clarity of explanation, appropriate program examples, strength and structure of argument, and use of references.
The text of the following code can be copied from the VLE under Assessment.
def a_project(projectID): supervisor = ??? # To do # def the_project(request): nonlocal supervisor if request == "supervisor": return lambda: supervisor elif request == "set_supervisor": return set_sv elif request == "projectID": return ??? # To do else: return "the_project: unknown request "+request # def set_sv(sv): nonlocal supervisor supervisor = sv return True # return the_project def project_supervisor(project): return project("supervisor")() def set_supervisor(project, supervisor): ??? #Todo def projectID(project): ??? #Todo
(本文出自csprojectedu.com,轉載請註明出處)