📂
CS-NoteBook
  • Introduction
  • CS144
    • concise introduction to Internet
      • 1.1 Networked Applications
      • 1.2 The 4 Layer Internet
      • 1.3 IP
      • 1.4 A Day in the Life of a Packet
      • 1.5 Principle: Packet Switching
      • 1.6 Principle:Layering
      • 1.7 Principle: Encapsulation
      • 1.8 Byte order and packet formats
      • 1.9 name and addresses:IPv4
      • 1.10 Longest Prefix Match for Link Layer
      • 1.11 Address Resolution Protocol(ARP)
      • 1.12 Summary
    • Transport Layer
      • 2.1 The TCP Service Model
      • 2.2 UDP service model
      • 2.3 ICMP(Internet Control Message Protocol 互联网报文控制协议)
      • 2.4 The End-to-End Principle
      • 2.5 Error Detection:3 schemes (Checksum,CRC and MAC)
      • 2.6 Finite State Machines(有限状态机)
      • 2.7 Flow Control
      • 2.8 Sliding window
      • 2.9 Retransmission Strategies
      • 2.10 TCP Header
      • 2.11 TCP Setup and Teardown
      • 2.12 Recap
    • Package Switching
      • 3.1 The history of Internet
      • 3.2 What is packet switching
      • [3.3 End-to-end delay and Queueing delay
      • 3.4 Playback Buffer(回放缓存区)
  • CS 61C
    • 1.4 C Memory Mangement, Usage
    • 1.5 Intro to Assembly Language, MIPS Intro
    • 1.5 extra bits operation
  • CS 61B
  • CS 61A
    • Function
    • Names
    • The Art of the Function
    • Control
    • Higher-Order Function
    • Recursive Function
    • List
    • Non-Local Assignment
    • Iterators
    • Objects
    • Data Abstraction
    • OOP
    • Inheritance
    • Representations
    • Decomposition
    • Scheme
    • Exceptions
    • Calculator
    • Interpreters
    • Declarative_Programming
    • Table
    • Aggregation
      • More_recursion
    • Databases
    • Distributed_Data
    • Tail Recursion
    • Exercises
      • lab00
      • lab01
      • hw01
      • tree Recursion example -- give Change
  • The Web DevelopMent Bootcamp
    • html5
    • css
    • bootstrap3
    • bootstrap4
    • javascript expression
    • javascript function
Powered by GitBook
On this page
  • Non-Local 细节
  • Python particulars(细节)
  • 其他可选的方法
  • 突变会导致 函数的引用透明性(referential transparency) 丢失

Was this helpful?

  1. CS 61A

Non-Local Assignment

  • 在函数内部创建的变量,只在自己的 Frame 里

  • 对于高阶函数,内部函数想要 引用/改变 parent frame 的变量值,则无法进行

  • nonlocal 声明变量使其绑定为 parent frame 的变量

def make_withdraw(balance):
    "Return a withdraw function with a starting balance."
    def withdraw(amount):
        nonlocal balance # declare the name "balance" nonlocal
        if amount > balance:
            return "Insufficient funds"
        balance -= amount
        return balance
    return withdraw

withdraw = make_withdraw(100)
withdraw(25)
withdraw(25)
wtihdraw(60)

Non-Local 细节

  • 将来对该 name 的赋值 将更改 其在绑定该 name 的当前环境中,第一个非本地框架(first non-local framework)中预先存在的绑定

  • 该 name 要已经被使用过了(已经存在)

  • 该 name 不能与 local scope 的 name 冲突

状态(x=2)

影响

没有nonlocal,x 没有在本地绑定

在当前环境的第一个 frame 中,创建一个 x 到对象 2 的新绑定

没有nonlocal,x 已经在本地绑定过

在当前环境的第一个 frame 中,将 x 重新绑定到对象 2

有nonlocal x,x 已经在 non-local frame 绑定过

在当前环境的第一个 non-local frame 中,将 x 重新绑定到对象 2

有nonlocal x,x 没有在 non-local frame 绑定过

语法错误

有nonlocal x,x 已经在 non-local frame 绑定过, x也在本地绑定过

语法错误

Python particulars(细节)

  • python 在执行方法内部前 -> 预先计算 每个 name 属于 哪个 frame

  • 在 function 内部,单一 name 的所有实例必须指向同一个框架

def make_withdraw(balance):
    """Return a withdraw function with a starting balance."""
    def withdraw(amount):
        if amount > balance:
            return 'Insufficient funds'
        balance = balance - amount  # 执行前计算,得知 balance 是本地变量
        return balance
    return withdraw
def make_withdraw(balance):
    """Return a withdraw function with a starting balance."""
    def withdraw(amount):
        if amount > balance:
            return 'Insufficient funds'
        # balance = balance - amount # 如果去掉,可以正常执行,因为这个 name 指向前一个 frame
        return balance
    return withdraw

其他可选的方法

  • 用可变值,如列表

def make_withdraw_list(balance):
    b = [balance]
    def withdraw(amount):
        if amount > b[0]:
            return 'Insufficient funds'
        b[0] = b[0] - amount
        return b[0]
    return withdraw

突变会导致 函数的引用透明性(referential transparency) 丢失

  • 函数的返回值只依赖于其输入值,这种特性就称为引用透明性

  • Mutation operations 违反了引用透明度的条件

    • 因为它们不仅仅是返回一个值,而是改变了环境

PreviousListNextIterators

Last updated 4 years ago

Was this helpful?