[ Web Proxy ]
URL:
Viewing: https://openhome.cc/Gossip/CodeData/JavaLambdaTutorial/Python.html [Back]  [Original]

Lambda/Closure3Python Lambda/Closure

Lambda/Closure3Python Lambda/Closure


English

Python def function
def max(m, n):
    return m if m > n else n

print(max(10, 3)) # print 10

maximum = max
print(maximum(10, 3)) # print 10
Python lambda
max = lambda m, n: m if m > n else n
print(max(10, 3)) # print 10
Lambda Python JavaScript
// JavaScript
function max(n, n) {
    return m > n ? m : n;
}

# Python
def max(m, n):
    return m if m > n else n

// JavaScript
function(n, n) {
    return m > n ? m : n;
};

# Python
lambda m, n: m if m > n else n
Lambda/Closure Closure Closure
import math
def prepare_factor(max):
    # Creating a prime table is time-consuming.
    primes = [i for i in range(2, max) if prime[i] == 1]

    def factor(num):
        while primes[i] ** 2 <= num:
            if num % primes[i] == 0:
                list.append(primes[i])
                num //= primes[i]
            else:
                i += 1

    return factor

factor = prepare_factor(1000)
print(factor(100)) # print [2, 2, 5, 5]
factor Closure primes primes primes
Python Closure Python Closure
def func():
    x = 10
    def getX():
        return x
    def setX(n):
        x = n   #  x
    return (gegX, setX)

getX, setX = func()
getX() # 10

setX(20)
getX(10) # still 10
Python setX setX x n func 10
Python 3 global nonlocal
def func():
    x = 10
    def getX():
        return x
    def setX(n):
        nonlocal x = n
    return (gegX, setX)

getX, setX = func()
getX() # 10

setX(20)
getX(10) # 20
nonlocal x Python x func x setX func x
JavaScript Python Lambda/Closure Lambda/Closure Scala Lambda/Closure







Web Proxy Viewer  |  New URL  |  Original Page