Chapter 1: Building Abstractions(抽象) with Functions
1.1 Getting Started
第1章:利用函数构造抽象
1.1 开始
Computer science is a tremendously(非常地) broad academic(学术的) discipline(学科). The areas of globally distributed(分布式的) systems, artificial(人造的) intelligence(智力), robotics(机器人学), graphics(图形), security, scientific computing, computer architecture(体系结构), and dozens of emerging(浮现) sub-fields all expand(扩张) with new techniques and discoveries every year. The rapid progress of computer science has left few aspects(方面) of human life unaffected. Commerce(贸易), communication, science, art, leisure(空闲的), and politics have all been reinvented(重新使用) as computational(计算的) domains.
计算机科学是一个非常宽广的学科。全球的分布式系统、人工智能、机器人学、图形学、安全、科学计算、计算机体系结构这些领域,还有,每年伴随着新技术和发现,许多浮现的子领域全部扩张。计算机科学的快速进步,使得人类生活方方面面受到了影响。贸易、交流、科学、艺术、休闲和政治,已经全部重新使用计算机领域。
The high productivity(生产力) of computer science is only possible because the discipline(学科) is built upon an elegant(高雅的) and powerful set of fundamental(基本的) ideas. All computing begins with representing information, specifying(指定) logic(逻辑) to process it, and designing abstractions that manage the complexity(复杂) of that logic(设计抽象,用以管理那种逻辑的复杂度). Mastering(精通) these fundamentals(原理;原则;基础) will require us to understand precisely(精确地) how computers interpret(解释) computer programs and carry out(执行;完成) computational processes.
计算机的高生产力,只可能是因为,这个学科建立在一个高雅的、强有力的基本理念集合。所有的计算开始于代表信息、指定逻辑以处理它,并且设计抽象以管理那种逻辑的复杂度。要精通这些原理,会要求我们去精确地理解,计算机如何解释计算机程序、执行计算过程。
These fundamental ideas(基本理念) have long been taught using the classic(经典的) textbook Structure(结构) and Interpretation of Computer Programs (SICP) by Harold Abelson and Gerald Jay Sussman with Julie Sussman. This text borrows heavily from that textbook, which the original authors have kindly licensed(同意;发许可证) for adaptation(改编,改写) and reuse under a Creative Commons license(许可证). These notes(注意事项;说明) are published(出版;公布) under the Creative(创造性的) Commons attribution(归因) non-commericial share-alike license version 3.
1.1.1 Programming in Python
1.1.1 在Python中编程
In order to define(定义) computational processes, we need a programming language; preferably(较好) one that many humans and a great variety of computers can all understand. In this text, we will work primarily(首要地,主要地) with the Python language.
Python is a widely used programming language that has recruited(vt.吸收某人为新成员) enthusiasts(狂热者) from many professions: web programmers, game engineers, scientists, academics, and even designers of new programming languages. When you learn Python, you join a million-person-strong community( 区) of developers. Developer communities( 区) are tremendously(极大地;非常) important institutions(机构): members help each other solve problems, share their projects and experiences, and collectively(共同地) develop software and tools. Dedicated(致力) members often achieve celebrity(名誉) and widespread esteem(尊重) for their contributions.
Python是一种广泛使用的编程语言,已经从很多专业领域吸引了爱好者:Web程序员、游戏工程师、科学家、学者,还有甚至新编程语言的设计者。当你使用Python,你加入了一个百万人的、强壮的开发者的 区。开发者 区是非常重要的机构:成员互相帮助、解决问题,分享他们的项目和经验,并且共同开发软件和工具。核心成员经常从他们的贡献中,获得名誉和尊重。
The Python language itself is the product of a large volunteer community that prides(为…自豪) itself on the diversity(多样性) of its contributors(贡献者). The language was conceived(怀孕) and first implemented(实施) by Guido van Rossum in the late 1980’s. The first chapter of his Python 3 Tutorial(教程) explains why Python is so popular, among the many languages available today.
Python编程语言自身,是一个庞大的志愿者 区的产品,该 区为它的贡献者的多样性而自豪。这个语言,在20世纪80年代末期,由Guido孕育,并首次实现。他的Python 3教程的第一章解释了,为什么在如今许多可以使用的语言的情况下,Python如此流行。
Python excels(优于;擅长) as an instructional(教学的) language because, throughout its history, Python’s developers have emphasized(强调) the human interpretability(可解释性) of Python code, reinforced(加强) by the Zen of Python guiding principles(原理) of beauty, simplicity(朴素), and readability(可读性). Python is particularly appropriate(适当的) for this text because its broad set of features(特色) support a variety of different programming styles, which we will explore. While(虽然) there is no single(单一的) way to program in Python, there are a set of conventions(惯例;公约) shared across the developer community that facilitate(促进) reading, understanding, and extending(扩展) existing(现有的) programs. Python’s combination(结合) of great flexibility(灵活性) and accessibility(易接近) allows students to explore many programming paradigms(范例), and then apply their newly acquired(获得) knowledge to thousands of ongoing(进行的;前进的) projects.
These notes maintain(维持) the spirit of SICP by introducing the features of Python in step with(in step with 与一致) techniques for abstraction(抽象) and a rigorous(严格的) model of computation(计算). In addition, these notes provide a practical introduction to Python programming, including some advanced language features and illustrative(说明的) examples. Increasing your facility(能力) with Python should come naturally as you progress through the text.
The best way to get started programming in Python is to interact(互相影响) with the interpreter(解释者) directly. This section describes how to install(安装) Python 3, initiate(开始) an interactive(交互式的) session(会话) with the interpreter, and start programming.
开始在Python中编程的最好的方式是直接使用交互式解释器。这一小节描述了如何安装Python 3,利用解释器开始一个交互式的会话,然后开始编程。
1.1.2 Installing Python 3
1.1.2 安装Python 3
As with(如同,和—–一样) all great software, Python has many versions. This text will use the most recent stable(稳定的) version of Python 3. Many computers have older versions of Python installed already, such as Python 2.7, but those will not match the descriptions in this text. You should be able to use any computer, but expect to install Python 3. (Don’t worry, Python is free.)
You can download Python 3 from the Python downloads page by clicking on the version that begins with 3 (not 2). Follow the instructions(操作指南) of the installer(安装程序) to complete installation(安装).
你可以从Python下载页面,通过点击以3(不是2)开始的版本,下载Python 3。遵从安装程序的操作指南,完成安装。
For further guidance, try these video tutorials(教程;辅导材料;使用说明书) on Windows installation and Mac installation of Python 3, created by Julia Oh.
想要更深的指导,尝试这些关于Windows和Mac安装Python 3的录像教程。
1.1.3 Interactive(交互式的) Sessions(会话)
1.1.3 交互式会话
In an interactive Python session, you type some Python code after the prompt(提示符), >>>. The Python interpreter reads and executes(执行;完成) what you type, carrying out(执行;完成) your various commands.
在一个交互式的Python会话中,你在【>>>】提示符后,输入一些Python代码。Python解释器读取和执行你输入的,执行你不同的指令。
To start an interactive session, run the Python 3 application. Type python3 at a terminal prompt(提示符) (Mac/Unix/Linux) or open the Python 3 application in Windows.
运行Python 3应用程序,以开始一个交互式会话。在终端提示符中输入Python 3,或者在Windows中打开Python 3应用程序。
If you see the Python prompt, >>>, then you have successfully started an interactive(交互式的) session(会话). These notes depict(描述) example interactions(相互作用) using the prompt, followed by some input(输入).
如果你看见Python 提示符【>>>】,然后你已经成功地开启了一个交互式的会话。这些注意事项描述了,紧跟着一些输入,使用提示符交互的例子。
Interactive controls. Each session keeps a history of what you have typed. To access that history, press -P (previous) and -N (next). -D exits a session, which discards(抛弃) this history. Up and down arrows also cycle through history on some systems.
交互式控制。每个会话保存了你已经输入的历史记录。为了获得历史记录,按Control-P(previous 较早的)和Control-N(next 下一个)。Control-D退出一个会话,抛弃历史记录。在一些系统中,上下光标也允许在历史记录中循环。
1.1.4 First Example
1.1.4 第一个例子
To give Python a proper introduction, we will begin with an example that uses several language features(特色). In the next section, we will start from scratch(乱写乱画;一款由MIT设计开发的一款面向少年的简易编程工具) and build up the language piece by piece. Think of this section as a sneak(暗中进行的) preview(预告片;预示;概述) of features to come.
为了给Python一个合适的介绍,我们将开始一个例子,这个例子使用几种语言特色。在下一小节,我们会从草稿开始,并且一点一点地构建这种语言。将这一小节作为即将到来的特性的一个暗中进行的概述。
Python has built-in(嵌入的) support for a wide range of common programming activities, such as manipulating(操纵) text, displaying graphics(图形), and communicating over the Internet. The line of Python code
Python已经内建支持范围广泛的普遍的编程活动,例如操纵文本,显示图形,以及在互联 上沟通。这行Python代码
is an import statement(声明) that loads functionality(功能) for accessing data on the Internet. In particular(尤其,特别), it makes available a function called urlopen, which can access the content at a uniform resource locator(定位器) (URL), a location of something on the Internet.
是一个重要的声明,加载了获取互联 上的数据的功能。特别的是, 它使一个称为urlopen的函数【可以获取URL上的内容】,互联 上的sth的位置。
Statements & Expressions. Python code consists of expressions and statements. Broadly, computer programs consist of instructions to either
声明和表达式。Python代码由表达式和声明组成。更宽泛地说,计算机程序也是由指令组成。
Statements typically(典型地; 代表性地; 通常) describe actions. When the Python interpreter(解释器) executes(执行;完成) a statement, it carries out the corresponding action. On the other hand, expressions typically describe computations(计算指令;计算). When Python evaluates(评价) an expression, it computes the value of that expression. This chapter introduces several types of statements and expressions.
声明通常描述了动作。当Python解释器完成了一个声明,它完成了相关的动作。换句话说,表达式通常描述了计算指令。当Python评价一个表达式,它计算了表达式的值。这一章介绍了几种类型的声明和表达式。
The assignment(指定的;工作;任务) statement
指定的声明
associates the name shakespeare with the value of the expression that follows =. That expression applies the urlopen function to a URL that contains the complete text of William Shakespeare’s 37 plays, all in a single text document.
将名字shakespeare与=之后的表达式的值相关联。那个表达式,把urlopen函数应用在一个URL,这个URL包含【William Shakespeare’s 37 plays】的全部文本,全部在一个单个的文档中。
Functions. Functions encapsulate(压缩) logic(逻辑) that manipulates(操纵) data. urlopen is a function. A web address is a piece of data, and the text of Shakespeare’s plays is another. The process by which the former(前者;在前的) leads to the latter may be complex(复杂的), but we can apply that process using only a simple expression because that complexity(复杂) is tucked() away(隐藏) within a function. Functions are the primary(首要的;主要的) topic of this chapter.
函数。函数压缩操纵数据的逻辑。urlopen是一个函数。一个web地址是一片数据,并且【莎士比亚】文本是另一个。前者导向后者的过程可能很复杂,但是我们可以仅仅使用一个简单的表达式,实现那个过程,因为那样的复杂性被隐藏在一个函数中。函数是本章首要的主题。
Another assignment(分配) statement(声明)
另一个指定的声明
associates the name words to the set of all unique(独特的) words that appear in Shakespeare’s plays, all 33,721 of them. The chain of commands to read, decode, and split, each operate on an intermediate(起媒介作用) computational(计算的) entity(实体): we read the data from the opened URL, then decode the data into text, and finally split the text into words. All of those words are placed in a set.
将名字words关联到所有独特的单词的集合,这些单词出现在【Shakespeare’s plays】,它们33,721个的所有。read、decode、split的指令链,每一个作用在一个媒介性的计算的实体:我们从公开的URL,读数据、把数据decode进文本、最终把文本split为单词。所有这些单词放置在一个集合中。
Objects. A set is a type of object, one that supports set operations like computing intersections(交叉) and membership(资格). An object seamlessly(无空隙地) bundles(收集) together data and the logic(逻辑) that manipulates(操纵) that data, in a way that manages the complexity(复杂) of both. Objects are the primary topic of Chapter 2. Finally, the expression
对象。一个集合是对象的一种类型,支持集合操作,比如计算交集和成员资格。一个对象无空隙地把数据收集到一起,并且这种逻辑操作那些数据,以一种方式,管理两者的复杂度。对象是第2章的首要的主题,最后,表达式
is a compound(复合的) expression that evaluates(评价;求值) to the set of all Shakespearian words that are simultaneously(同时地) a word spelled in reverse(相反). The cryptic(神秘的) notation(符 ) w[::-1] enumerates(列举) each letter in a word, but the -1 dictates(命令) to step backwards. When you enter an expression in an interactive(交互式的) session(会话), Python prints its value on the following line.
是一个复合的表达式,该表达式求值所有的【Shakespearian】单词,相反地,这些单词同时也是一个单词。这个神秘的符 w[::-1]列出一个单词中的每个字母,但是【-1】命令往回走。当你进入一个交互式会话的表达式,Python在下一行打印它的值。
Interpreters. Evaluating(评价;求值) compound(复合的) expressions requires a precise(精确的) procedure(程序) that interprets(解释) code in a predictable(可预言的) way. A program that implements(实现;执行) such a procedure, evaluating compound expressions, is called an interpreter. The design and implementation(实现) of interpreters is the primary topic of Chapter 3.
解释器。求值复合表达式需要一个精确的程序,这个程序以一种可预期的方式,解释代码。一个【执行了这样的一个过程的】程序,求出复合表达式的值,被称为一个解释器。解释器的设计和实现是第3章的首要的主体。
When compared with other computer programs, interpreters for programming languages are unique(特别的;独特的) in their generality(在一般性上). Python was not designed with Shakespeare in mind. However, its great flexibility(灵活性) allowed us to process a large amount(数量) of text with only a few statements(声明) and expressions.
当和其他计算机程序比较的时候,编程语言的解释器,在一般性上,是特别的。Python没有把莎士比亚设计在脑子里面。然而,它的巨大的灵活性,允许我们以只是很少的声明和表达式,去处理一个庞大数量的文本。
In the end, we will find that all of these core concepts(思想;概念) are closely related: functions are objects, objects are functions, and interpreters are instances(实例) of both. However, developing a clear understanding of each of these concepts and their role in organizing code is critical(极重要的;关键的) to mastering the art of programming.
在结尾,我们会发现,所有这些核心的思想是紧密相关的:函数是对象,对象是函数,并且解释器是两者的实例。然而,发展这些思想和在组织代码中组织它们的角色的,一个清晰的理解,对于掌握编程的艺术,是极重要的。
1.1.5 Errors
1.1.5 错误
Python is waiting for your command. You are encouraged to experiment with the language, even though you may not yet know its full vocabulary(词汇;词汇表) and structure(结构). However, be prepared for errors. While computers are tremendously(非常地) fast and flexible(灵活的), they are also extremely rigid(严格的). The nature of computers is described in Stanford’s introductory(引导的) course as
Python正在等待你的命令。你可以用这种语言做实验,尽管你可能还不知道它的全部语法和结构。然而,为错误做好准备。虽然计算机非常快和灵活,它们也非常严格。计算机的天性,在斯坦福的导论课程中被描述为
The rigidity(严格) of computers will immediately become apparent(显现;可看见的) as you experiment with the Python interpreter(解释器): even the smallest spelling and formatting(格式化) changes will cause unexpected(意外的) output(输出) and errors.
当你用Python解释器做实验的时候,计算机的严格会立即显现:即使是最小的拼写和格式化改变会引发意外的输出和错误。
Learning to interpret errors and diagnose(诊断) the cause of unexpected errors is called debugging. Some guiding principles(原理) of debugging(调试以排除故障) are:
学会解释错误和诊断意外错误的引发,被称作debugging。一些debugging的指导原则是:
1.Test incrementally(逐渐地): Every well-written program is composed(构成) of small, modular(模块化的) components(成分) that can be tested individually(个别地). Try out(试验;试用) everything you write as soon as possible to identify(确定) problems early and gain confidence in your components.
1.逐渐地测试:每个写得好的程序由小的、模组化的成分构成,这些成分可以单独测试。你一写好,就立刻试验每一件东西,来及早地确定问题,并且在你的组件中获取信心。
2.Isolate(隔离) errors: An error in the output of a statement(声明) can typically(代表性地;典型地;通常) be attributed(归属) to a particular(特别的) modular component. When trying to diagnose a problem, trace(追踪) the error to the smallest fragment(片段) of code you can before trying to correct(改正) it.
2.隔离错误:在一个声明的输出中的错误,通常可以归属于一个特别的模组成分。当尝试诊断一个问题,在尝试改正一个问题之前,追踪错误到最小的代码片段。
3.Check your assumptions(假设): Interpreters do carry out(完成;执行) your instructions(指令) to the letter — no more and no less. Their output is unexpected(想不到的;意外的;突然的) when the behavior of some code does not match what the programmer believes (or assumes) that behavior to be. Know your assumptions, then focus your debugging effort on verifying that your assumptions actually hold.
3.检查你的假设:解释器确实执行你的指令到字母 —— 不多也不少。当一些代码的行为不符合程序员相信的(或者假设的)应该是的那样的行为,它们的输出是想不到的。了解你的假设,然后将你的debugging努力,专注于核实你的假设实际上所支撑的。
4.Consult(咨询) others: You are not alone! If you don’t understand an error message, ask a friend, instructor(指导书), or search engine. If you have isolated(孤立的) an error, but can’t figure out how to correct it, ask someone else to take a look. A lot of valuable programming knowledge is shared in the process of group problem solving.
4.咨询其他人:你不是一个人!如果你不理解一个错误信息,问一个朋友、指导书、或者搜索引擎。如果你有一个孤立的错误,但是不能找出如何改正它,去叫别人看看。许多有价值的编程知识是分享在解决问题的团体的过程中。
Incremental(增加的) testing, modular design, precise(精确的) assumptions, and teamwork are themes that persist(存留) throughout this text. Hopefully, they will also persist throughout your computer science career(事业).
Continue: 1.2 Elements(元素) of Programming
继续:1.2 编程的元素
参考文献:
1.http://composingprograms.com/
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!