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

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

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 @ 2022-08-20 19:50  xgqfrms  阅读(29)  评论(2编辑  收藏  举报