cs61a笔记
- None -> print().输出是print这种no pure函数的side effect
- 一个函数调用另一个函数,形成了不同local frame。每个local frame有每一层的执行的namescape。互相没有联系。global frame是local frame的关系。
- 函数调用时,参数传入会在local frame
- assert,判断输入是否合理
- 当function的return是另一个fuction时。
传多个参数,首先是外层函数,其次传给return function的函数。传参数->return function->传参数
两次调用函数,就是两层local frame
- why higher order fuction?
- 抽象共同特征
- 减少关系连接
- lambda没有statement,也没有name。
01 if gay else straight
- return lambda嵌套函数,调用图
- disc
- Local Names are not visable to other funcs. A调用B, A和B的参数是互不交集的。 A嵌套B,A是B的parent frame,A的参数对B是visable
- lambda区别于function:没name
(lambda:3)() 3
调用函数时候才会形成local frame。local frame先在本层找,没有再去global frame找。
self reference 返回自身,递归调用?
- 调用函数前,把所有参数evaluate完。
- decocrators
trace functions.可以自定义显示的trace内容.
- mututal recursion
- if else,在无return的if中,else最好不要省略
- iteration -> recursion
把每次iterate重复的部分抽象封装。自顶向下展开函数后 自底向上return
call 直到return才结束
- no return - > none
trace 代替print
- tree recursion video in 2018
-
change iteration to simpler by 'in'
expression是可循环value,etc list. 顺序: 将每次循环的value绑定到name,然后再执行下面的suite
-
Range(代替计数器(index))
- length: end - start
- 范围: 包含start,不包含end
- 只有一个参数时,第一个参数为0
- [1:] 参数+: --> 最后一个参数到end
-
- 用递归compute,展开计算,递归返回底层结果。
-
reverse
- 抽象出共同特点作为扩展的case
-
recurse 从小到大递归,先找到base case(最大值),再递归到更小的case
- 将返回结果作为函数的参数
- recurse --> iteration:
- 将返回结果作为函数的参数
-
help function在外面能调用,里面省去了重复的参数。
-
sequence aggregation
constructor -> define 某种对象 selector -> 选择数据结构中的value
constructor and selector
- 用数据结构存储需要的值,selector来选取值,用定义的计算方式来计算。 第一层抽象 第二层 第三层:w
- slice 是用range取指定范围内映射的简化
- 在某范围内,找映射函数的最大值
- iterable类 基础computation
sequence aggregation(内置在iterable 数据结构里的函数
tree: label brach, tree --> list
- use tree to do recursion, need to sum all branches
- 遍历tree上所有的分支 求和
- 用tree来做递归,与过去递归差别在:它是普通递归的封装,普通递归只是调用本身函数,tree的base case一般是leaf,recursion是遍历branch,value是label。
- tail recursion
- n是一个计数器,k是个result的过程值。(如何写递归多参数)
- tree - constructor.tree 由label和branches组成。tree是constructor,label和braches是selector。
- fib tree --> tree recursion(分开相加,有选择) 也是tree,在tree本身结构上递归。
遍历所有brach,总和。
关于tree最好的图解!!!
- ASCII --> 英语 Unicode --> all languages
- pop remove类似切片的黑盒化
pop return 最后一个value
-
list in list, 之间的应用关系借助一个空list[]
-
- 只有slice类选中赋值可以插入list,否则只是value。插入list是put value,而不是container s[0:0] --> insert s[0:1] --> replace and insert
- tuple,进行操作后不会改变本身。list相反。
- Iterators
- iter --> return 一个 iterator
- next --> 返回下一个
- yield用在某些return后还继续操作的情况下
- result[1]
- list --> generator
- result[1]
- 当创建一个instance时发生的,self绑定到创建的instance上,传入的参数绑定到__init__的参数上.
-
- getattr(instance_name, 'attrname')
-
- hasattr(instance_name, 'attrname')
- inherience不是copy attribute,而是向上访问base类的attribute
- how to look up attribute
- this class
- base class
- how to look up attribute
- inherience -
- 使用已有的方法。进而扩展
- 复写
- multiple inherience
- 可以有多个父类
- repr nad repr
- repr --> class attribute
- link list -> add new link/chage first and add new empty list
- recusre in link list
- 写很多题要看construct
- scheme - fractal
- built in and user defined procedure
- try exception
-
REPL --> Read-Eval-Print-Loop
- Read --> Turns input into a useful data structure
- Literal(1)
- Name('x')
-
the types of expressions in PyCombinator --> literal, name, call expression, lambda expression
-
the types of values in PyCombinator --> number, lambda function, primitive function
-
A lambda function is the result of evaluating a lambda expression
-
-
-
-
Chapter 1
1st
- 变量类型必须有,且不可改变,在运行前verify
- A func is called a method
- define func --> public staic
- func's parameters and return value must declared type
- Only one return val
2nd
- 没有main的method可以在有main的method里被调用
原生的写法-->method
-
static
- 如果子class不用夫class的instance的method或val。那即声明为static
-
private
- safe to change private method(implement)
Add last
得考虑到list为null的情况
sentinel node
1.