Lecture 4

A. Creating function:

1. Syntax

def<function name> (<formal parameters>) :

      <function body>

• def is a keyword

• Name is any legal Python name

• Within parenthesis are zero or more formal parameters – each is a variable name to be used inside function body

2. some points

•Expressions for each parameter are evaluated, bound to formal parameter names of function

•Control transfers to first expression in body of function

•Body expressions executed until return keyword reached (returning value of next expression) or run out of expressions (returning None)

•Invocation is bound to the returned value

•Control transfers to next piece of code

3. docstring is a special type of comment that is used to document what your function is doing.Typically, docstrings will explain what the function expects the type(s) of the argument(s) to be, and what the function is returning.

4. Properties

– Decomposition: Break problems into modules that are self-contained, and can be reused in other settings

– Abstraction: Hide details. User need not know interior details, can just use as if a black box.

5. consideration from finger problem:

-False, None, 0, 0.0, empty array usually consider as "false"

-Python it is legal to compare functions!

-any legal python name could be considered as function


-如果对应function中已经给了赋值,那么按照给定赋值run,如果没有,就用local binding。


-str. lower () --->将str的大写转换为小写字母

B. Specifications:"""XXXXXXX"""

1. Are a contract between implementer of function and user

– Assumptions: conditions that must be met by users of function. Typically constraints on parameters, such as type, and sometimes acceptable ranges of values

– Guarantees: Conditions that must be met by function, provided that it has been called in way that satisfies assumptions

c. string method

Objects are special because we can associate special functions, referred to as object methods, with the object. 

https://docs.python.org/3/library/stdtypes.html#string-methods

http://www.greenteapress.com/thinkpython/html/thinkpython009.html#toc93

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,900评论 0 23
  • 不好意思再一次打扰到您了,我是昨天中午面试【物流培训生】的应届生。自从昨天面试回来,内心里一直觉得很不舒服,我觉得...
    小太阳战斗阅读 422评论 0 0
  • 利用官方的registry镜像搭建本地私有仓库! [root@docker ~]# docker run -d -...
    如来自然阅读 1,142评论 1 2
  • 今天有点特别。 早晨时下了很大的雨,天空有大片的乌云,顿时有了年少时北方夏季里酣畅的暴雨之感,还有种不可名的奇异感...
    阿叶禾阅读 301评论 0 0
  • 每天多给自己点微笑,再多点鼓励,你可以的,fighting
    29b749870607阅读 128评论 0 1