Python - Day5 (function)

0. Contents

1. Function call definition
2. The function - i.e. flexible and powerful
3. Types of actual parameter
4. The function of the variable scope (local and global variables)
shielding variable mechanism 5.
6. vivo function to modify global variables --Global

1. Definition and function calls

Here's a very simple to write a very basic function:

>>> def myfirstfunction():
	print('这是我的第一个函数!')
	print('我很激动!')

>>> myfirstfunction()
这是我的第一个函数!
我很激动!

2. function - that is powerful and flexible

  1. Function Documentation
  2. Keyword arguments
  3. The default parameters
  4. Collection parameters
>>> def speaking(name,words): #关键字参数
	print(name + '->' + words)

	
>>> speaking('小甲鱼','让编程改变世界')
小甲鱼->让编程改变世界
>>> speaking('让编程改变世界','小甲鱼')
让编程改变世界->小甲鱼
>>> speaking(words = '让编程改变世界',name = '小甲鱼')
小甲鱼->让编程改变世界

>>> def saysome(name = '小甲鱼',words = '让编程改变世界'):
	print(name + '->' + words )  #默认参数

	
>>> saysome()
小甲鱼->让编程改变世界
>>> saysome('最小生成树')
最小生成树->让编程改变世界
>>> saysome('最小生成树','二叉树')
最小生成树->二叉树

>>> def test(*params):
	print('参数的长度是:',len(params));
	print('第二个参数是:',params[1]);

	
>>> test(1,'小甲鱼',3.2,5,6,7,8)
参数的长度是: 7
第二个参数是: 小甲鱼
>>> def test(*params,exp):
	print('参数的长度是:',len(params),exp);
	print('第二个参数是:',params[1]);

	
>>> test(1,'小甲鱼',3.2,5,6,7,exp = 8)
参数的长度是: 6 8
第二个参数是: 小甲鱼
>>> test(1,'小甲鱼',3.2,5,6,7,8)
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    test(1,'小甲鱼',3.2,5,6,7,8)
TypeError: test() missing 1 required keyword-only argument: 'exp'
>>> def test(*params,exp = 8):
	print('参数的长度是:',len(params),exp);
	print('第二个参数是:',params[1]);

	
>>> test(1,'小甲鱼',3.2,5,6,7,8)
参数的长度是: 7 8
第二个参数是: 小甲鱼

3. argument difference between Form

1, the function is defined in different ways
parameter appears in a function definition, function can be used throughout the body, leaving the function can not be used.
Appears in the argument in the calling function, after entering the called function, the argument variables can not be used.

2, using the principles of different
shape function parameters listed in the function declaration, a function in the body of the function definition. When the function call, parameters (of any kind) is a kind of blank to be filled or a placeholder.
Argument is used to fill a parameter. When the function is called, parameters are listed in parentheses after the function name. A function call, is transmitted to the argument parameter.

3, call-by-call references and different
call-by-call and a reference parameter refers to a mechanism for the transfer process. Call by value, only the value of the argument. Call mechanism by value, the parameter is a local variable whose initial value is the value of the respective arguments. In reference call mechanism, the address of the argument passed to the parameter, the surface is replaced by a variable argument parameter, so any change in the parameter actually takes place on the argument variable.

4. The scope of the variable function

Specifically, the difference between the global variable (Global Variable) and local variables (local Variable) as follows:

  1. Different scope: the scope of global variables for the entire program, and the scope of local variables for the current function or circulation

  2. Different storage memories: the global variables stored in the global data area, local variables are stored in the stack area

  3. Different life cycle: life cycle global variables and the main program, as with the destruction process and destruction, local variables within a function or internal circulation, with exit or exit loop function does not exist

  4. Different ways: global variable parts of the program can be used in the statement, but local variables can only be used locally. Internal function will prefer to use local variables and then use global variables


>>> def discounts(price,rate):
	final_price = price * rate
	old_price = 88 #这里试图修改全局变量
	print('修改后全局变量的值:',old_price)
	return final_price

>>> old_price = float(input('请输入原价:'))

>>> rate = float(input('请输入折扣率:'))

>>> new_price = discounts(old_price,rate)

>>> print('修改后old_price的值位:',old_price)

>>> print('打折后价格是:',new_price)

请输入原价:100
请输入折扣率:0.8
修改后全局变量的值: 88
修改后old_price的值位: 100.0
打折后价格是: 80.0
#这里可以看到两个old_price不一样,虽然名称相同,
#但是函数体内的是局部变量,外面的是全局变量

The variable masking mechanism

>>> count = 5
>>> def func():
	count = 10
	print(10)

	
>>> func()
10
>>> print(count)
5

After global variable defined, the function will modify global variables masked operation.

6.Global keywords - function in vivo modify global variables

>>> def func():
	global count 
	count = 10
	print(10)

	
>>> func()
10
>>> print(count)
10
Published 23 original articles · won praise 14 · views 714

Guess you like

Origin blog.csdn.net/weixin_45253216/article/details/104529537