Python程式設計入門學習筆記(五)
阿新 • • 發佈:2018-12-25
### 函式 ```python varibal = { 'a': 100, 'b': 100, 'c': 200 } ``` ```python varibal['a'] ``` 100 ```python varibal.items() ``` dict_items([('a', 100), ('b', 100), ('c', 200)]) ```python #尋找value的值為100的key值 [key for key, value in varibal.items() if value == 100] ``` ['a', 'b'] #### 函式 - 抽象概念 ```python def get_keys(dict_varibal,value): return [k for k, v in dict_varibal.items() if v == value] ``` ```python get_keys(varibal,200) ``` ['c'] #### 函式是組織好的,可重複使用的,能夠完成特定功能的程式碼塊,它是程式碼塊的抽象。 ```python get_keys({'a': 40}, 40) ``` ['a'] #### 位置引數是不可以交換位置的 ```python def get_keys(dict_varibal,value): return [k for k, v in dict_varibal.items() if v == value] ``` - get_keys 函式名 - ()中為引數:dict_varibal——形參,呼叫的時候傳遞的值才是實參 - return 是返回值 1、位置引數 2、關鍵字引數,可以不按照順序去寫 ```python get_keys(dict_varibal={'a':40},value=40) ``` ['a'] ```python get_keys(value=40,dict_varibal={'a':40}) ``` ['a'] ### 函式通過引數獲取我們傳遞的值,函式中改變了引數的值,那麼我們傳遞進去的值會改變麼? ```python def test(varibal): varibal = 100 return varibal ``` ```python var = 1 test(var) ``` 100 ```python #var變數的值沒有改變 print(var) ``` 1 ```python def test(varibal): varibal.append(100) return varibal ``` ```python var = [] test(var) ``` [100] ```python #var變數的值發生了改變 print(var) ``` [100] #### 不建議對可變型別在函式內進行更改,建議用函式返回值進行重新賦值 ```python def test(varibal): temp = varibal.copy() temp.append(100) return temp ``` ```python var = [] var = test(var) ``` [100] ```python var ``` [] ### 引數的收集 ```python #*args收集位置引數, **kwargs收集關鍵字引數 def test(name, age, *args, **kwargs): print(name, age, *args, **kwargs) ``` ```python test('wang', 12) ``` wang 12 ```python test('wang', 12, 23, 'lkl',[23,34]) ``` wang 12 23 lkl [23, 34] ```python dict_varibal = { 'weight' : 120, 'height' : 175 } test('wang', 12, dict_varibal) ``` wang 12 {'weight': 120, 'height': 175} #### 【重要】裝飾器 ```python a = 10 b = [12,12] def test(): print('test') c = test ``` #### 可以把函式賦值給一個變數 ```python c.__name__ ``` 'test' ```python def test(func): return func def func(): print('func run') f = test(func) f.__name__ f() ``` func run #### 函式可以當做函式的返回值進行返回 ```python import random #返回一個從0到1的浮點值 def test(): return round(random.random(), 3) ``` ```python # 函式返回的浮點值保留三位有效數字 ``` ```python test() ``` 0.112 ### Python中的另一個語法糖,裝飾器 ```python #返回一個從0到1的浮點值 @decorator def test(): return random.random() @decorator def test_two(): return random.random()*10 ``` ```python def decorator(func): def wrapper(*args,**kwargs): # do something return round(func(*args,**kwargs), 3) return wrapper ``` ```python #該語句完全等價於裝飾器@decorator的寫法 # f = decorator(test) ``` ```python f() ``` 0.18173988944007524 ```python f.__name__ ``` 'wrapper' ```python test.__name__ ``` 'wrapper' ```python test() ``` 0.033 ```python test_two() ``` 2.714 ### 類 ```python class Person: def __init__(self, name, age): self._name = name self._age = age def get_name(self): return self._name def rename(self, new_name): self._name = new_name ``` #### 初始化函式中,self後面的是例項化物件的屬性,加下劃線的意思是,代表這個屬性是私有的,不應該訪問 ```python s = 'hello world' s.center(12) ``` 'hello world ' ```python p = Person('wang', 12) ``` ```python p.get_name() ``` 'wang' ```python p.rename('wang lei') ``` ```python p.get_name() ``` 'wang lei' ```python p_2 = Person('li', 11) p_2.get_name() ``` 'li' #### pass代表什麼都不做,只是佔個位而已 ```python # class Student(Person): # pass ``` ```python class Student(Person): def set_score(self, score): self._score = score def get_score(self): return self._score ``` ```python s = Student('liu', 24) s.get_name() ``` 'liu' ```python s.set_score(100) ``` ```python s.get_score() ``` 100 ```python class Person: def __init__(self, name, age): self._name = name self._age = age @property def name(self): return self._name def rename(self, new_name): self._name = new_name ``` ```python p = Person('liu', 24) p.name ``` 'liu'