Function face questions Collection

Whether used in a function functools? What is his role?
  1. functools.wraps()
    • In the decorator used, if you do not use wraps, and the value __name__ __doc__ original function will be lost
  2. functools.reduce()
    • The first parameter is a function of the second parameter is an iterative object code is as follows:
# 1 was added to the code below corresponds. 9
 from functools Import the reduce  A = the reduce ( the lambda X, Y: X + Y, Range ( 10)) Print (A)
How to determine a value of a method or function?

Reference links

  1. Use type () to determine if the method is a method, if a function is a function.
  2. Examples of classes and function without binding relationship belong to the function (function)
  3. There are binding relationship between class and instance methods belong to the function
Please write a function to convert the ip addresses into an integer. The 10.3.9.12 converted 0,000,101,000,000,011 0,000,100,100,001,100, then converted into an integer
def ip2int(ip):
    nums=ip.split('.') # zfill()函数是补0 to_bin=[bin(int(i))[2:].zfill(8) for i in nums] return int(''.join(to_bin),2) i=ip2int('127.0.0.1') print(i)
lambda expressions formats and scenarios?
  • Format: lambda parameter list: return expression
  • Scenario: common usage in the filter, reduce and map.
pass use
  • Usually used to mark a location yet to write code, pass without doing anything, generally used for placeholder sentence, maintaining the integrity of the program structure
* Arg and ** kwargs role
  • For receiving a number of uncertain parameters, * args typically used to receive the number of non-key uncertain parameters, and for receiving a keyword normally ** kwargs number of uncertain parameters
How to set a global variable in a function?
  • Use the keyword global variables defined in the function
The results beg the following code:
def num():
    return [lambda x:i*x for i in range(4)] print([m(2) for m in num()])
  • Answer: [6,6,6,6]
The difference between the yield and yield from

Brief yield and yield from

# 下面a()和b()是等价的
def a():
    yield from [1,2,3,4,5] def b(): for i in [1,2,3,4,5]: yield i for i in a(): print(i) for i in b(): print(i)
  • It will yield a function generator into a
  • yield a return value
  • followed by the iteration may yield from the object, a return a value.
The following code is output demand
collapse=True
processFunc=collapse and (lambda s:' '.join(s.split())) or (lambda s:s) print(processFunc('i\tam\ntest\tproject!')) collapse=False processFunc=collapse and (lambda s:' '.join(s.split())) or (lambda s:s) print(processFunc('i\tam\ntest\tproject!'))
  • answer:
i am test project! i am test project!
Write a function to find the array and there are no duplicate values
def func(lis):
    lis1=[]
    del_lis=[]
    for i in lis: if i not in lis1: if i not in del_lis: lis1.append(i) else: del_lis.append(i) lis1.remove(i) return sum(lis1) def func2(lis): return sum([i for i in set(lis) if lis.count(i)==1]) print(func2([3,4,1,2,5,6,6,5,4,3,3]))
Execution of the following code is
a=1
def bar():
    a+=3 bar() print(a)
  • Answer: Run Error
Write a function to calculate the numbers represent the letters, each letter is not the same value
for A in range(1,10):
    for B in range(10): if A==B: continue for C in range(1,10): if C in [A,B]: continue for D in range(10): if D in [A,B,C]: continue for E in range(1,10): if E in [A,B,C,D]: continue for F in range(10): if F in [A,B,C,D,E]: continue for G in range(1,10): if G in [A,B,C,D,E,F]: continue for H in range(10): if H in [A,B,C,D,E,F,G]: continue for P in range(1,10): if P in [A,B,C,D,E,F,G,H]: continue if (A*10+B)-(C*10+D)==(E*10+F) and (E*10+F)+(G*10+H)==(P*100+P*10+P): print(A,B,C,D,E,F,G,H,P)

The output code written as follows

Reference links

def decorator_a(func):
    print('Get in decorator_a') def inner_a(*args, **kwargs): print('Get in inner_a') return func(*args, **kwargs) return inner_a def decorator_b(func): print('Get in decorator_b') def inner_b(*args, **kwargs): print('Get in inner_b') return func(*args, **kwargs) return inner_b @decorator_b #f=decorator_b(f) @decorator_a #f=decorator_a(f) def f(x): print('Get in f') return x * 2 f(1)
  • answer
Get in decorator_a Get in decorator_b Get in inner_b Get in inner_a Get in f
  • Explanation
When we make a call to f 1 parameter passing, inner_b is called, he will first print Get in inner_b, then inside inner_b called inner_a, it will then print Get in inner_a, and then call the original interior inner_a f, and the result is returned as a final summary: decorator function to execute when executed from top to bottom up to perform the function call from immediately after the function definition is a good decoration
The output write the following code:
def test():
    try:
        raise ValueError('something wrong') except ValueError as e: print('error occured') return finally: print('ok') test()
  • The results (finally will be executed no matter what)
error occured ok
The output of the following code is obtained
mydict={'a':1,'b':2} def func(d): d['a']=0 return d func(mydict) mydict['c']=2 print(mydict)
  • result
{'a': 0, 'b': 2, 'c': 2}
Write a function to receive a folder name as a parameter, shows the path of a file folder, and the file contained in the folder now
# 方法一
import os
def Test1(rootDir): list_dirs = os.walk(rootDir) for root, dirs, files in list_dirs: for d in dirs: print(os.path.join(root, d)) for f in files: print(os.path.join(root, f)) Test1(r'C:\Users\felix\Desktop\aaa') print('###################') # 方法二 import os def Test2(rootDir): paths=os.listdir(rootDir) for lis in paths: path=os.path.join(rootDir,lis) print(path) if os.path.isdir(path): Test2(path) Test2(r'C:\Users\felix\Desktop\aaa')

Guess you like

Origin www.cnblogs.com/yhq123/p/11360860.html