必定要会的新技术功能

快速交换(Quick swap)

# in python, also works in ES6

s1 = 3
s2 = 4

# Quick swap
s1, s2 = s2, s1

惰性计算(Lazy evaluation)

是指仅仅在真正须要执行的时候才计算表达式的值。javascript

  1. 避免没必要要的计算,带来性能的提高。
    对于条件表达式 if x and y,在x为false的状况下y表达式的值将再也不计算。而对于if x or y,当x的值为true的时候将直接返回,再也不计算y的值。所以编程中能够利用该特性:
    • and 逻辑中,将小几率发生的条件放在前面;
    • or 逻辑中,将大几率发生的时间放在前面,有助于性能的提高。
  2. 节省空间,使得无线循环的数据结构成为可能。
    Python中最经典的使用延迟计算的例子就是生成式表达器了,它在每次须要计算的时候才经过yield产生所须要的元素。
    例:斐波那契数列在Python中实现起来很容易,使用yied对于while True也不会致使其余语言中所遇到的无线循环问题。
def fib():
   a,b = 0,1
   while True:
       yield a
       a,b = b,a+b
               
fib_gen = fib()

for i in range(30):
    print(fib_gen.next())

解构赋值(Destructuring assignment)

# In python

# Destruction
li = (1, 2, 3)
a, b, c = li

print(b)

li1 = [1, 2, 3, 4, 5, 6]
c, *_, d = li1
// In ES6
// we have an array with the name and surname
let arr = ["Ilya", "Kantor"]

// destructuring assignment
let [firstName, surname] = arr;

alert(firstName); // Ilya
alert(surname);  // Kantor

Get details from site ES6 destructuring assignment.java

相关文章
相关标签/搜索