Python第二模块总结

1.函数传参时使用当前实参的引用,还是重新创建的值?

答:使用当时实参的引用

2.Python的什么为作用域?

  A.缩进的代码块   B.函数

答:B

3.简述深浅拷贝的原理?

答:

4.简述sys.path的作用以及模块命名注意事项

BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

sys.path.append(BASE_DIR) 

https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules/#id16

5.简述re模块中match、search以及findall方法的特点

 s = """life is short, life is good, life is beauty!""" 
 print(re.match(r'is', s)) 
 print(re.search(r'is', s)) 
 print(re.findall(r'life is (w+)', s)) 
 print(re.sub(r'life', 'wife', s)) 
6.写代码实现:

1*2+3*4+5*6+7*8...+99*100

7.Python中两种序列化json和pickle的区别

8.

        有如下变量。

            l1 = ["alex", 123, "eric"]
            l2 = ["alex", 123, 'eric']

            s1 = """ ["alex", 123, "eric"] """
            s2 = """ ["alex", 123, 'eric'] """

        请判断以下四个操作是否正确?

            a. json.loads(l1)
            b. json.loads(l2)
            c. json.loads(s1)
            d. json.loads(s2)

            e. json.dumps(l1)
            f. json.dumps(l2)
            g. json.dumps(s1)
            h. json.dumps(s2) 
9.写装饰器
         如有一下两个函数,请书写一个装饰器实现在不改变函数调用者的代码基础上,实现在函数执行前后分别打印"before" 和 "after"。

        def f1(arg):
            return arg + 1

        def f2(arg1, arg2):
            return arg1 + arg2
两种写法区别:
 1 def encode(func):
 2     def wrapper(*args, **kws):
 3         print('encode')
 4         func(*args, **kws)
 5     return wrapper
 6  
 7  
 8 @encode
 9 def sendMsg(text):
10     print(text)
11  
12 if __name__ == '__main__':
13     sendMsg('hello world')
 1 def encode(type):
 2     def decorator(func):
 3         def wrapper(*args, **kws):
 4             print('use %d encode' % type)
 5             func(*args, **kws)
 6         return wrapper
 7     return decorator
 8  
 9  
10 @encode(1)
11 def sendMsg(text):
12     print(text)
13  
14 if __name__ == '__main__':
15     sendMsg('hello world')

首先encode内部有两层函数定义,是一个高阶函数,decorator的定义就是要返回的装饰器函数,encode函数以sendMsg函数为参数,在内部封装一个新函数返回,该函数添加相应的预处理方法encode,可以继续二次调用。(前者)

如果有新的要求,即encode函数自身有参数,一个整形type来表示使用何种加密方法进行加密,这个时候就要修改上述写法,在encode中加一层函数。(后者)

 

10.书写代码,创建6位的随机验证码(含数字和字母)
 
 
原文地址:https://www.cnblogs.com/gqq-0504/p/6929192.html