Superimposing a plurality of decorators, yield recursive expressions, ternary expressions, formula, function

Superposition of multiple decorators

# A superimposed load multiple decorators, run the analysis (understanding ***) 

# DEF deco1 (func1): # func1 = wrapper2 memory address
# DEF wrapper1 (* args, ** kwargs):
# Print ( 'being run ===> deco1.wrapper1 ')
# RES1 = func1 (* args, ** kwargs)
# return RES1
# return wrapper1

# DEF deco2 (func2): # = func2 wrapper3 memory address
# def wrapper2 (* args, * kwargs *):
# Print ( 'running ===> deco2.wrapper2')
# RES2 = func2 (* args, ** kwargs)
# return RES2
# return wrapper2

# DEF deco3 (X):
# DEF outter3 (func3) : # func3 = memory address index function decorative objects
# DEF wrapper3 (* args, ** kwargs):
# Print ( 'running ===> deco3.outter3.wrapper3')
# RES3 = func3 (args *, * * kwargs)
RES3 return #
# return wrapper3
# return outter3

# load order from bottom to top (to know)
# # @ deco1 index = deco1 (wrapper2 memory address) ===> index = wrapper1 memory address
# @ deco2 # index = deco2 ( wrapper3 memory address) ===> index = wrapper2 memory address
# @ deco3 (111) # === > @ outter3 ===> index = outter3 (index) ===> index = wrapper3 memory address
# def index (X, Y):
# Print ( 'index from% S:% S'% (X, Y))

# perform top-down order, i.e. wraper1- "wrapper2-" wrapper3
# index (1,2) # wrapper1 (1,2)

yield expression

# X = yield Return Value 

# a:
# DEF Dog (name):
# Print ( '% s Columbia Road ready to eat it ...'% name)
# the while True:
# # X got the yield received value
# x = yield # x = 'meat bun'
# Print ( '% s eat Columbia Road% s'% (name, X))
#
#
# Dog G = ( 'Alex')
# g.send (None) equivalent to # Next (G)
#
# g.send ([ 'a bone', 'AAA'])
# # g.send ( 'meat bun')
# # g.send ( 'together swill')
# G # .close ()
# # g.send ( '1111') can not be closed after the traditional values #

# two:
# DEF Dog (name):
# food_list = []
# Print ( '% s Columbia Road ready to eat it ... "% name)
# the while True:
# # X got the yield value of the received
# x = yield food_list # x = ' meat buns'
# Print ( '% s eat Columbia Road% s'% (name, x ))
# Food_list.append (x) # [ 'a bone', 'meat bun']
#
# Dog G = ( "Alex")
# RES = g.send (None) # Next (G)
# Print (RES)
#
# res = g.send ( 'a bone')
# Print (RES)
#
# res = g.send ( 'meat bun')
# Print (RES)
# # g.send ( 'together swill')

# DEF FUNC ():
# Print ( 'Start .....')
# # 1111 the yield X = X = 'XXXXX'
# Print ( 'ha aha')
# Print ( 'ha aha')
# Print ( ' Ah ha ')
# Print (' ha aha ')
# 22222 the yield

# G = FUNC ()
# Next RES = (G)
# Print (RES)
#
# g.send RES = (' XXXXX ')
# print (res)

A triplet of expressions

The following requirements for # 
# DEF FUNC (X, Y):
# IF X> Y:
# X return
# the else:
# return Y
#
# FUNC RES = (1,2)
# Print (RES)

# ternary expressions with
# Syntax format: value if condition else condition when the condition is established to be returned is not satisfied to return value
# X =. 1
# Y = 2

# RES = X if X> Y else Y
# Print (RES)

# RES = 111111 if 'Egon' == 'Egon' the else 2222222222
# Print (RES)

# application example
# DEF FUNC ():
# # IF. 1>. 3:
# = # X. 1
# # the else:
# = # X. 3
#
# X = IF. 1. 1> 3 else 3

Generative

# 1, formula list 
# L = [ 'alex_dsb', 'lxx_dsb', 'wxx_dsb', "xxq_dsb", 'Egon']
# new_l = []
# for L in name:
# name.endswith IF ( 'DSB' ):
# new_l.append (name)

# new_l = [L IF name in name for name.endswith ( 'DSB')]
# new_l = [name in name for L]

# Print (new_l)

# all changed to all lowercase letters uppercase
# new_l = [name.upper () for name in L]
# Print (new_l)

# remove all name suffix _dsb
# new_l = [name.replace ( '_ DSB', '') for name in L]
# Print (new_l)

# 2, dictionary formula
# Keys = [ 'name', 'Age', 'Gender']
# DIC = {Key: None for Key in Keys}
# Print (DIC)

# items = [( ' name ',' egon '), (' age ', 18), ('gender','male')]
# res={k:v for k,v in items if k != 'gender'}
# Print (RES)

#. 3, the collection formula
# Keys = [ 'name', 'Age', 'Gender']
# setl = {Key for Key in Keys}
# Print (setl, type (setl))

#. 4, generator expression
# = G (I for I in Range (10) IF I>. 3)
#! ! ! ! ! ! ! ! ! ! ! Emphasize! ! ! ! ! ! ! ! ! ! ! ! ! ! !
# Now g interior of a value not

# Print (g, type (g))

# Print (g)
# Print (Next (g))
# Print (Next (g))
# Print (Next (g))
# Print ( Next (G))
# Print (Next (G))
# Print (Next (G))
# Print (Next (G))


# with Open ( 'notes .txt', mode = 'rt' , encoding = 'utf- . 8 ') AS F:
# a way:
# RES = 0
# Line for in F:





# Print (RES)

# Three ways: the most efficient
# RES = SUM ((len (Line) for Line in F))
# above-can be abbreviated as follows
# RES = SUM (len (Line) for Line in F)
# Print (res)

Recursive function

# A: recursive definitions 
# recursive function calls: is a special form of nested function calls
# specifically means:
# calling a function call process has itself directly or indirectly to the

# direct calls itself
# def f1 ():
# Print ( 'I was I was I')
# f1 ()
# f1 ()

# indirect access calls itself
# DEF f1 ():
# Print ( '===> f1')
# F2 ()
#
DEF F2 # ():
# Print ( '===> F2')
# F1 ()
#
# F1 ()

loop program running in two piece of code #
# a ways: while, for loop
# the while True:
# Print (1111)
# Print (2222)
# Print (3333)

(B) # mode: recursive nature is cyclic:
# DEF f1 ():
# Print (1111)
# Print (2222)
# Print (3333)
# f1 ()
# f1 ()

# Two: the need to emphasize is this:
# recursive call should not be unlimited call it, must end a recursive call in to meet certain conditions,
# the n-= 0
# the while the n-<10:
# Print (the n-)
# the n-+ = 1

# F1 DEF (n-):
# n-IF == 10:
# return
# Print (n-)
# =. 1 + n-
# F1 (n-)
#
# F1 (0)

# three: two recursive phases
# backtracking: one layer down call
# recursion: certain termination condition is satisfied, the recursive call ends, and then return one level

# Age (. 5) = Age (. 4) + 10
# Age (. 4) = Age (. 3) + 10
# Age (. 3 ) = Age (2) 10 +
# Age (2) Age = (. 1) 10 +
# Age (. 1) = 18 is

# DEF Age (n-):
# == n-IF. 1:
# 18 is return
# return Age (N- . 1) 10 +
#
#
# Age RES = (. 5)
# Print (RES)

# four: applied recursively
L = # [1,2, [. 3, [. 4, [. 5, [. 6, [. 7, [. 8, [9,10,11, [12 is, [13 is,]]]]]]]]]]
#
DEF f1 # (List1):
# for the X-in List1:
# IF of the type (the X-) iS list:
# # If the list, it should be recycled, and then determine that the re-run their own code
# f1 (the X-)
# the else:
# Print (X)
#
# F1 (L)

Guess you like

Origin www.cnblogs.com/0B0S/p/12566208.html