1, what is the Magic method in Python?
The Magic method is the nickname of the overloaded operator, in the form of a __init__ similar to the front and back double glide lines, commonly used __init__,__new__,__call__,__str__,__getitem__ ... Wait a bunch. (After a while I'll update my blog about how these operators are used.) ）
2, what is closure, and what does the adorner have to do with it?
An inner function is defined in an outer function, and a temporary variable of the outer function is used in the inner function, and the return value of the outer function is a reference to the inner function. This makes up a closure.
You old iron, do not understand Ah, that is right. Then look down and see how the decorator is written (adorners are the best embodiment of closures)
What is that, a decorator? is to add extra functionality to the original program without changing the original code, and to reduce the duplication of code.
Import TimedefDeco (func):defwrapper (): StartTime=time.time () func () EndTime=time.time () msecs= (endtime-starttime) *1000Print("Time is %d Ms"%msecs)returnWrapper@decodeffunc ():Print("Hello") Time.sleep (1)
Slightly basic children should understand, is to set a function outside, in the original function plus @ Decorator name!
If it is ignorant now, then let you become ignorant of it!
Import TimedefRegister (active=True)defDeco (func):defwrapper ():ifActive:starttime=time.time () func () EndTime=time.time () msecs= (endtime-starttime) *1000Print("Time is %d Ms"%msecs)Else: func ()returnwrapperreturndeco @deco (active=False)deffunc ():Print("Hello") Time.sleep (1)
This is set up a switch, judging, if it is true, calculate the time, otherwise, does not count the time.
L = [1,3.,5,7,9= L.sort ()
How much is l now, please?
L = [1,3.,5,7,9= l.sort ()print(l)>>>none
A lot of beginner's little friends may be confused.
This is because the direct call to sort is sorted on the original basis, there is no return value!!
How do you want to get it?
L = [1,3.,5,7,9= sorted (l)print(l)>>>[0, 1, 3.0, 5, 7, 9]
Four, what is the difference between a generator and an iterator?
The essence of the generator is the iterator, and the generator is essentially the keyword of yield in the function.
The difference between yield and return is that the function encounters yield and does not end, just hang, but return is over.
Yield has a send () method, which can be assigned a value of yield
Five, write a Fibonacci algorithm:
def fib (n): = 0, 1 while a < N: Print(A, end=") = B, A + b Print (a) fib (1000)
Six,< '. * > and < '. *? ' > Difference?
Greedy match and non-greedy match, the former match as long as possible, the latter match as short as possible.
Seven, count the frequency of each word in an English article and return the first 10 words with the highest frequency and the number of occurrences
1 fromCollectionsImportCounter2 ImportRe3 4With open ('a.txt','R', encoding='Utf-8') as F:5TXT =F.read ()6c = Counter (Re.split ('\w+', TXT))#Remove the number of occurrences of each word7 Print(c)8ret = C.most_common (10)#the top 10 most frequently removed9 Print(ret)
Eight, washed and slept, trapped dead, back to update
Write a common question and answer about Python development interview, keep updating--see mood