Python function, iteration

1. Function

1. Nesting of functions

. 1  DEF huangwei ():   # function can not inside that do not perform outside 
2      name = ' Huang ' 
. 3      Print (name)
 . 4  
. 5      DEF Liuyang ():
 . 6          name = ' Liuyang ' 
. 7          Print (name)
 . 8  
. 9          DEF nulige ():
 10              name = ' fahsfha ' 
11              print (name)
 12  
13          print (name)
 14          nulige ()
 15          return nulige
 16 
17      liuyang ()
 18      print (name)
 19      return liuyang

 

(1) Only huangwei () order is 1-2-3-17-5-6-7-13-14-9-10-11-15-18-19

(2) huangwei () = liuyang, so to run liuyang should be huangwei () ()

(3) huangwei () () = nulige, so to run hard, brother should be huangwei () () ()

2. Self-loop of function

 1 person_list = ['a', 'b', 'v', 'd', 'vd']
 2 
 3 def ask_way(person_list):
 4     print('-' * 60)
 5     if person_list == 0:
 6         return '根本没人知道'
 7     person = person_list.pop(0)
 8     if person == 'd':
 9          return  ' That way is. . . . . ' 
10      Print ( ' hi% S, you know the way you xx ' % the Person)
 11      Print ( ' % S I'm not sure I can help you ask S% ' % (the Person, person_list))
 12      Import Time
 13      the time.sleep ( 0)   # time to import 
14      res = ask_way (person_list)
 15      return res
 16  "" " ----------------------------- -------------------------------
 17  hi a, do you know xx road
 18 a I do n’t know, let me ask you ['b', 'v', 'd', 'vd']
 19  ----------------------- -------------------------------------
 20  hi b, do you know xx way
 21  b I don't Know that I can help you ask ['v', 'd', 'vd']
 22  ------------------------------ ------------------------------
 23  hi v, do you know xx way
 24  v I do n’t know, I ’ll ask you [ 'd', 'vd']
 25  ----------------------------------------- -------------------
 26  That way is. . . . .
27  "" "

3. Lambda function

(1) Anonymous functions generally use lambda x: x + 1: x as a formal parameter in conjunction with other functions, x + 1 is the return value 

  (2) Functional programming, there should not be any assignment in the function body, no variable definition

 

Second, iteration

y = x .__ iter __ () converts x to an iterable object, which can be converted to an iterator

y .__ next __ () / next (y) Iterate the output of each element, one line of code outputs one, if too much, it will report an error

Yield of yield function is equivalent to return but return can only be executed once, yield can be executed multiple times

def test ():
     yield S   # yield is equivalent to return 
    yield 1
     yield 2
     yield 3   # yield can be executed multiple times, but return can only be executed once 
g = test ()
 print (g)
 print ((g. __next__ ()))
 Print ((G. __next__ ()))
 Print ((G. __next__ ()))
 Print ((G. __next__ ()))
 # results 
# <Generator Object Test AT 0x10e240f20> 
# {. 1, 2,. 3} 
# . 1 
# 2
#3

 

Guess you like

Origin www.cnblogs.com/adelinebao/p/12749458.html