Python编程入门学习笔记(五)
### 函数
```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'