小言_互联网的博客

lecture 2 Names|CS 61A: Structure and Interpretation of Computer Programs Spring 2020

329人阅读  评论(0)

Names

# Imports
from math import pi
pi * 71 / 223
from math import sin
sin(pi/2)

# Assignment
radius = 10
2 * radius
area, circ = pi * radius * radius, 2 * pi * radius
radius = 20

# Function values
max
max(3, 4)
f = max
f
f(3, 4)
max = 7
f(3, 4)
f(3, max)
f = 2
# f(3, 4)
__builtins__.max

# User-defined functions
from operator import add, mul

def square(x):
    return mul(x, x)

square(21)
square(add(2, 5))
square(square(3))

def sum_squares(x, y):
    return add(square(x), square(y))
sum_squares(3, 4)
sum_squares(5, 12)

# area function
def area():
    return pi * radius * radius
area()
radius = 20
area()                #radius是全局变量。因为radius之前被定义为10,之后又改变为20,所以两次调用"area"结果不同
radius = 10
area()               

# Name conflicts
def square(square):
    return mul(square, square)
square(4)      # 查看一个变量在内存中存储的值,首先查看本地,再查看全局

Evaluating Nested Expressions

>>> sub(pow(2, add(1, 10)), pow(2, 5))
2016

Defining Functions

Assignment is a simple means of abstraction: binds names to values
Function definition is a more powerful means of abstraction: binds names to expressions

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (version 1):

  1. Add a local frame, forming a new environment
  2. Bind the function’s formal parameters to its arguments in that frame
  3. Execute the body of the function in that new environment

A function’s signature has all the
information needed to create a local frame

Looking Up Names In Environments

Every expression is evaluated in the context of an environment.
So far, the current environment is either:
• The global frame alone, or
• A local frame, followed by the global frame.
Most important two things I’ll say all day:

  • An environment is a sequence of frames.
  • A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found
    E.g., to look up some name in the body of the square function:
    • Look for that name in the local frame.
    • If not found, look for it in the global frame.
    (Built-in names like “max” are in the global frame too,
    but we don’t draw them in environment diagrams.)

1.2.6 The Non-Pure Print Function

Pure functions

Functions have some input (their arguments) and return some output (the result of applying them). The built-in function

>>> abs(-2)
2

意味着该函数中有return 关键字,会return一个非“”none“”值
none 相当于java中的空指针null
The function abs is pure. Pure functions have the property that applying them has no effects beyond returning a value. Moreover, a pure function must always return the same value when called twice with the same arguments.

Non-pure functions

In addition to returning a value, applying a non-pure function can generate side effects, which make some change to the state of the interpreter or computer. A common side effect is to generate additional output beyond the return value, using the print function.

>>> print(1, 2, 3)
1 2 3

While print and abs may appear to be similar in these examples, they work in fundamentally different ways. The value that print returns is always None, a special Python value that represents nothing. The interactive Python interpreter does not automatically print the value None. In the case of print, the function itself is printing output as a side effect of being called.

A nested expression of calls to print highlights the non-pure character of the function.

>>> print(print(1), print(2))
1
2
None None

子表达式print(1), print(2)首先打印1,2 ,并返回两个“None“值给最外层的print


转载:https://blog.csdn.net/yaoxinJJJ/article/details/105384789
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场