Python 3 function & *args & **kwargs All In One
#!/usr/bin/env python3
# coding=utf-8
__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__copyright__ = """
Copyright (c) 2012-2050, xgqfrms; mailto:[email protected]
"""
"""
/**
*
* @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
refs
https://www.freecodecamp.org/news/args-and-kwargs-in-python/
https://book.pythontips.com/en/latest/args_and_kwargs.html
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/281128.html