python resolves the problem that the function returns return

  • 2021-08-12 03:13:00
  • OfStack

To define a function with return value, you need to use return statement to return a target value when calling this function. When there is no return, the function returns None by default.

Analyze the following two programs:


def now():
  print('2017-9-25')
now()

out:

2017-9-25


def now():
  print('2017-9-25')
print(now())

out:

2017-9-25

None

For the first program, only the 'now ()' function is called and 'print (' 2017-9-25 ')' is executed, while the second function executes' print (now ()) ', which first calls the' now () 'function and executes' print ('2017-9-25') ', and then prints the return value of the name () function, that is, None. The second function is equal to


def now():
  print('2017-9-25')
x=now() # Call now () function, which executes print('2017-9-25') , and then now The return value of the function None Assign x
print(x)# Print out x

Supplementary knowledge: python, add waiting time in the cycle, so as to wait for 1 period of time at any time after each cycle

When the crawler crawls the web page data, sometimes it is found by the server because the access frequency is too regular, and the access timeout or ip is blocked.

Therefore, every round of crawling, followed by a waiting time at any time, can reduce the probability of being found

random and time libraries are mainly used

The implementation script is as follows


import random
import time

for a in range(10):
  b = random.randint(1,10)# Random from 1 To 10 Internal fetching 1 Integer value 
  print(a)
  time.sleep(b)# Pass the randomly fetched integer value to the wait function 

Related articles: