xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Python function argument All In One

Python function argument All In One

Python 函数参数

https://docs.python.org/3/library/typing.html

https://docs.python.org/3/library/typing.html#typing.ParamSpec.args

arbitrary positional arguments (*args 不定位置参数)
arbitrary keyword arguments (**kwargs 不定关键字参数)

python function argument types

  1. default arguments
  2. keyword arguments
  3. positional arguments
  4. arbitrary positional arguments (*args 不定位置参数)
  5. arbitrary keyword arguments (**kwargs 不定关键字参数)

https://levelup.gitconnected.com/5-types-of-arguments-in-python-function-definition-e0e2a2cafd29

https://pynative.com/python-function-arguments/

强制位置参数

Python 3.8 新增了一个函数形参语法:

/, 用来指明前面的函数形参必须使用指定位置参数,不能使用关键字参数的形式;
*, 用来指明后面的函数形参必须使用指定关键字参数,不能使用位置参数的形式;

在以下的例子中,a 和 b 必须使用位置形参 ,c 或 d 可以是位置形参或关键字形参,而 e 和 f 必须使用关键字形参:

def f(a, b, /, c, d, *, e, f):
    print("位置参数", a, b)
    print("c 或 d 可以是位置形参或关键字形参", c, d)
    print("关键字参数", e, f)
    # print(a, b, c, d, e, f)

# 正确的使用方法 ✅
f(10, 20, 30, d=40, e=50, f=60)

# 错误的使用方法 ❌
# b 不能使用关键字形参
f(10, b=20, c=30, d=40, e=50, f=60)

# e 不能使用位置形参
f(10, 20, 30, 40, 50, f=60)

https://www.runoob.com/python3/python3-function.html

demos

#!/usr/bin/env python3
# coding: utf8

def func_args(arg1: int, arg2: str = None, arg3: float = 1.0, arg4: bool = True):
  print("arg1 {}".format(arg1))
  print("arg2 {}".format(arg2))
  print("arg3 {}".format(arg3))
  print("arg4 {}".format(arg4))
  print("\n")

# 全部使用匿名参数 ✅
func_args(60, "LEDs", 0.2, False)

# 全部使用匿名参数 ✅
func_args(arg1=60, arg2="LEDs", arg3=0.2, arg4=False)

# 混合使用,前面匿名参数,后面具名参数 ✅
func_args(60, "LEDs", arg3=0.2, arg4=False)

# 混合使用,前面匿名参数,中间具名参数,后面匿名参数 ❌
# func_args(60, "LEDs", arg3=0.2,  False)
# Positional argument cannot appear after keyword arguments Pylance ❌

"""
positional argument / keyword arguments
https://www.cnblogs.com/xgqfrms/p/17451650.html#5182186
https://docs.python.org/3/library/typing.html
"""

$  chmod +x ./function-pass-multi-args.py
$ ./function-pass-multi-args.py
arg1 60
arg2 LEDs
arg3 0.2
arg4 False

arg1 60
arg2 LEDs
arg3 0.2
arg4 False

arg1 60
arg2 LEDs
arg3 0.2
arg4 False

image

#!/usr/bin/env python3
# coding: utf8

def f(a, b, /, c, d, *, e, f):
    print("位置参数", a, b)
    print("c 或 d 可以是位置形参或关键字形参", c, d)
    print("关键字参数", e, f)
    # print(a, b, c, d, e, f)

# 正确的使用方法 ✅
f(10, 20, 30, d=40, e=50, f=60)

# 错误的使用方法 ❌
# b 不能使用关键字形参
f(10, b=20, c=30, d=40, e=50, f=60)

# e 不能使用位置形参
f(10, 20, 30, 40, 50, f=60)

$ pyhthon3 ./function-mix-args.py
# OR
$ py3 ./function-mix-args.py
位置参数 10 20
c 或 d 可以是位置形参或关键字形参 30 40
关键字参数 50 60
Traceback (most recent call last):
  File "/Users/xgqfrms-mm/Documents/github/Raspberry-Pi/Pi-4B/ws2812b-led-strip/./function-mix-args.py", line 15, in <module>
    f(10, b=20, c=30, d=40, e=50, f=60)
TypeError: f() got some positional-only arguments passed as keyword arguments: 'b'

image

refs

https://www.cnblogs.com/xgqfrms/p/17451650.html#5182186

https://www.cnblogs.com/xgqfrms/p/16603099.html



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(17)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2022-06-02 SwiftUI App Dark Mode All In One
2022-06-02 Xcode code folding ribbon All In One
2022-06-02 SwiftUI extension Bundle for parse JSON file All In One
2021-06-02 手动搭建 TypeScript + React + Webpack + Babel 的开发框架 All In One
2020-06-02 call & apply
2020-06-02 js & bitwise-operators
2020-06-02 js repeatify & no for loop All In One
点击右上角即可分享
微信分享提示