调用函数/创建函数
def my_func(arg1):
print('hello myFunc')
print('arg1:', arg1)
my_func('abc')
参数 (参数配置)
- 关键字参数, 指定参数名进行赋值
print('abc', end='\n')
def func(a, b, c):
print(a)
print(b)
print(c)
func(c=1, a=2, b=3)
- 变长参数
# 变长参数
def how_long(first, *other):
return 1 + len(other)
print(how_long(1, 2, 3, 4))
作用域
var1 = 123
def func():
var1 = 456
print(var1)
func()
print(var1)
# 输出
456
123
- 更改全局变量
var1 = 123
def func():
global var1
var1 = 456
print(var1)
func()
print(var1)
# 输出
456
456