網(wǎng)站首頁 編程語言 正文
基本語法
在Python中,在函數(shù)參數(shù)不確定數(shù)量的情況下,可以使用如下方式動(dòng)態(tài)在函數(shù)內(nèi)獲取參數(shù),args實(shí)質(zhì)上是一個(gè)list,而kwargs是一個(gè)dict
def myFun(*args, **kwargs):
在Go語言中,也有類似的實(shí)現(xiàn)方式,只不過Go中只能實(shí)現(xiàn)類似*args的數(shù)組方式,而無法實(shí)現(xiàn)**kwargs的方式。實(shí)現(xiàn)這種方式,其實(shí)也是利用數(shù)組的三個(gè)點(diǎn)表達(dá)方式,我們這里來回憶一下。
關(guān)于三個(gè)點(diǎn)(…)Ellipsis的說明
我們經(jīng)常在Go中看到這種方式,首先三個(gè)點(diǎn)的英文是Ellipsis,翻譯成中文叫做“省略”,可能各位看到這個(gè)詞就比較好理解三個(gè)點(diǎn)的作用了。在不同的位置上有不同的作用,比如在上述數(shù)組的定義中,省略了數(shù)組長度的聲明,而是根據(jù)數(shù)組初始化值來決定。在函數(shù)定義中,我們還會(huì)看到類似的使用方法,我們?cè)龠M(jìn)行詳細(xì)的說明。
其實(shí)本質(zhì)上三個(gè)點(diǎn)的表達(dá)方式就是利用數(shù)組這一特性,實(shí)現(xiàn)可變參數(shù)。來看一下定義格式:
// arg will be [...]int func myfunc(arg ...int) {} // paras will be [...]string func myfunc(arg, paras ... string) {}
示例一:函數(shù)中獲取可變參數(shù)
循環(huán)獲取可變參數(shù),并且將部分arguments傳入子函數(shù)
package main import "fmt" func myfunc(arg ... string) { fmt.Printf("arg type is %T\n", arg) for index, value := range arg { fmt.Printf("And the index is: %d\n", index) fmt.Printf("And the value is: %v\n", value) } } func main() { myfunc("1st", "2nd", "3rd") }
對(duì)上面的例子進(jìn)行分析:
可變參數(shù)arg類型為[]string
通過for進(jìn)行循環(huán)并獲取值
arg type is []string
And the index is: 0
And the value is: 1st
And the index is: 1
And the value is: 2nd
And the index is: 2
And the value is: 3rd
示例二:將切片傳給可變參數(shù)
我們?cè)谏厦娉绦虻幕A(chǔ)上實(shí)現(xiàn)一個(gè)新的函數(shù)mySubFunc,嘗試將切片(Slice)傳遞給該函數(shù)
package main import "fmt" func myfunc(arg ... string) { fmt.Printf("arg type is %T\n", arg) for index, value := range arg { fmt.Printf("And the index is: %d\n", index) fmt.Printf("And the value is: %v\n", value) } // Call sub funcation with arguments fmt.Printf("Pass arguments: %v to mySubFunc\n", arg[1:]) mySubFunc(arg[1:] ...) } func mySubFunc(arg ... string) { for index, value := range arg { fmt.Printf("SubFunc: And the index is: %d\n", index) fmt.Printf("SubFunc: And the value is: %v\n", value) } } func main() { myfunc("1st", "2nd", "3rd") }
我們來分析一下這段代碼:
1.與上面的代碼大部分邏輯相同,這里利用切片arg[1:]獲取部分可變參數(shù)的值
2.在傳輸給子函數(shù)mySubFunc()時(shí),使用了這樣的表達(dá)方式mySubFunc(arg[1:] …),這里補(bǔ)充一下…對(duì)于切片用法的說明
… signifies both pack and unpack operator but if three dots are in the tail position, it will unpack a slice.
在末尾位置的三個(gè)點(diǎn)會(huì)unpack一個(gè)切片
示例三:多參數(shù)
我們?cè)賮砜匆粋€(gè)多參數(shù)的例子
package main import "fmt" func myfunc(num int, arg ... int) { fmt.Printf("num is %v\n", num) for _, value := range arg { fmt.Printf("arg value is: %d\n", value) } } func main() { myfunc(1, 2, 3) }
來分析一下這個(gè)代碼:
函數(shù)參數(shù)一個(gè)為整型變量num,和可變變量arg
主函數(shù)中第一個(gè)參數(shù)為num,而后面的則存儲(chǔ)于arg中
所以輸出結(jié)果如下
num is 1
arg value is: 2
arg value is: 3
原文鏈接:https://blog.csdn.net/xiaoquqi/article/details/125816759
相關(guān)推薦
- 2023-01-27 Python?Flask利用SocketIO庫實(shí)現(xiàn)圖表的繪制_python
- 2022-12-05 Go?reflect?反射原理示例詳解_Golang
- 2022-10-07 Unity游戲開發(fā)實(shí)現(xiàn)場景切換示例_C#教程
- 2023-03-18 pandas預(yù)處理部分地區(qū)數(shù)據(jù)案例_python
- 2022-09-06 React父組件調(diào)用子組件中的方法實(shí)例詳解_React
- 2022-08-18 Python壓縮包處理模塊zipfile和py7zr操作代碼_python
- 2022-04-05 詳解C#如何實(shí)現(xiàn)讀寫ini文件_C#教程
- 2022-05-24 Python?3.x踩坑實(shí)戰(zhàn)匯總_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支