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

Python 3 function & *args & **kwargs All In One

Python 3 function & *args & **kwargs All In One

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

demos


#!/usr/bin/env python3
# coding=utf-8

__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__copyright__ = """
  Copyright (c) 2012-2050, xgqfrms; mailto:xgqfrms@xgqfrms.xyz
"""

"""
  /**
   *
   * @author xgqfrms
   * @license MIT
   * @copyright xgqfrms
   * @created 2022-08-19
   *
   * @description
   * @augments
   * @example
   * @link
   *
  */
"""




def func(arg1, arg2, *args, arg5):
  print(arg1, arg2)
  print(*args)
  print(type(*args))
  # tuple 元组
  print(arg5)
  return None;

# *args 后面的参数必须是命名参数 ✅
func(1,2, [3, 4], arg5 = 5)
# func(1,2, 3, 4, arg5 = 5)
# TypeError: type() takes 1 or 3 arguments ❌ print(type(*args))

print('\n')
# **kwargs 后面的不能再有任何参数了 ✅
def test(arg1, arg2, **kwargs):
  print(arg1, arg2)
  print(kwargs)
  print('type =', type(kwargs))
  # dict 字典
  for key, value in kwargs.items():
    print(key, '=', value)
  return None;

test(1,2, third = 3, four = 4)



function


def sum(arg1, arg2):
  # print(arg1, arg2)
  return arg1 + arg2;

total = sum(1,2)
print(total)
# 3

lambda function

匿名函数

类似 js ES6, Arrow Function


total = (lambda arg1, arg2 : arg1 + arg2)(1, 2)
print(total)
# 3

*args

不定参数 / 不定匿名参数

类似 js ES6, ...rest


def func(arg1, arg2, *args, arg5):
  print('\n')
  print(arg1, arg2)
  for arg in args:
    print(arg)
  print(arg5)
  return None;

func(1,2, 3, 4, 5)


#!/usr/bin/python3
# -*- coding: UTF-8 -*-

def func(arg1, arg2, *args, arg5):
  print('\n')
  print(arg1, arg2)
  # print(type(*args))
  print(*args)
  print(arg5)
  return None;

# func(1,2, 3, 4, 5)
# TypeError: func() missing 1 required keyword-only argument: 'arg5' ❌

# *args 后面的参数必须是命名参数 ✅
func(1,2, [3, 4], arg5 = 5)
func(1,2, 3, 4, arg5 = 5)
# TypeError: type() takes 1 or 3 arguments ❌

"""
1 2
[3, 4]
5

1 2
3 4
5
"""

**kwargs

不定命名参数


#!/usr/bin/python3
# -*- coding: UTF-8 -*-

print('\n')

# **kwargs 后面的不能再有任何参数了 ✅
def test(arg1, arg2, **kwargs):
  print(arg1, arg2)
  for key, value in kwargs.items():
    print(key, '=', value)
  return None;

test(1,2, third = 3, four = 4)

# 1 2
# third = 3
# four = 4

"""

def test(arg1, arg2, **kwargs):
  print(arg1, arg2)
  for arg in **kwargs
    print(arg)
  return None;

test(1,2, third = 3, four = 4)
# SyntaxError: invalid syntax

def test(arg1, arg2, *kwargs):
  print(arg1, arg2)
  print(*kwargs.third)
  print(*kwargs.four)
  return None;

test(1,2, third = 3, four = 4)
# TypeError: test() got an unexpected keyword argument 'third'❌


def test(arg1, arg2, *kwargs, arg5):
  print(arg1, arg2)
  print(*kwargs.third)
  print(*kwargs.four)
  return None;

test(1,2, third = 3, four = 4, 5)
# SyntaxError: positional argument follows keyword argument ❌
"""

Python REPL

#!/usr/bin/python
# -*- coding: UTF-8 -*-


#!/usr/bin/python3
# -*- coding: UTF-8 -*-

https://www.runoob.com/try/runcode.php?filename=helloworld_cn&type=python

https://www.runoob.com/try/runcode.php?filename=helloworld_cn&type=python3

demos

refs

https://www.freecodecamp.org/news/args-and-kwargs-in-python/

https://book.pythontips.com/en/latest/args_and_kwargs.html



©xgqfrms 2012-2021

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

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


posted @   xgqfrms  阅读(30)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2021-08-20 RUM technologies All In One
2021-08-20 npm registry All In One
2020-08-20 SVG 与 Canvas 对比 All In One
2020-08-20 Elastic Search 原理剖析
2020-08-20 js logical operation All In One
2020-08-20 逆波兰表达式,是什么鬼
2020-08-20 js bitwise operation All In One
点击右上角即可分享
微信分享提示