调用函数/创建函数

  1. def my_func(arg1):
  2. print('hello myFunc')
  3. print('arg1:', arg1)
  4. my_func('abc')

参数 (参数配置)

  • 关键字参数, 指定参数名进行赋值
  1. print('abc', end='\n')
  2. def func(a, b, c):
  3. print(a)
  4. print(b)
  5. print(c)
  6. func(c=1, a=2, b=3)
  • 变长参数
  1. # 变长参数
  2. def how_long(first, *other):
  3. return 1 + len(other)
  4. print(how_long(1, 2, 3, 4))

作用域

  1. var1 = 123
  2. def func():
  3. var1 = 456
  4. print(var1)
  5. func()
  6. print(var1)
  7. # 输出
  8. 456
  9. 123
  • 更改全局变量
  1. var1 = 123
  2. def func():
  3. global var1
  4. var1 = 456
  5. print(var1)
  6. func()
  7. print(var1)
  8. # 输出
  9. 456
  10. 456