Python第二模塊總結

1.函數傳參時使用當前實參的引用,仍是從新建立的值?python

答:使用當時實參的引用json

2.Python的什麼爲做用域?app

  A.縮進的代碼塊   B.函數ide

答:B函數

3.簡述深淺拷貝的原理?ui

答:google

4.簡述sys.path的做用以及模塊命名注意事項加密

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

sys.path.append(BASE_DIR) 

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

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位的隨機驗證碼(含數字和字母)
相關文章
相關標籤/搜索