Python的 *args 和 **kw是不是和C/C++的(int argc, char *argv[])类似呢?
如题,有人来解释一下吗?
如题,有人来解释一下吗?
2016-09-30
*args是非关键字参数,用于元组,**kw是关键字参数,用于字典 1.*args def tupleArgs(arg1, arg2= 'B', *arg3): print('arg 1:%s ' % arg1) print('arg 2:%s ' % arg2) for eachArgNum in range(len(arg3)): print('the %d in arg 3 :%s ' % (eachArgNum,arg3[eachArgNum])) if __name__ == '__main__': tupleArgs('A') # arg 1:A # arg 2:B tupleArgs('23','C') # arg 1:23 # arg 2:C tupleArgs('12','A','GF','L') # arg 1:12 # arg 2:A # the 0 in arg 3 :GF # the 1 in arg 3 :L **kw def dictArgs(kw1, kw2= 'B', **kw3): print('kw 1:%s ' % kw1) print('kw 2:%s ' % kw2) for eachKw in kw3: print('the %s ---->:%s ' % (eachKw,kw3[eachKw])) if __name__ == '__main__': dictArgs('A') # kw 1:A # kw 2:B dictArgs('23','C') # kw 1:23 # kw 2:C dictArgs('12','A', c = 'C',d = '12121212') # kw 1:12 # kw 2:A # the d ---->:12121212 # the c ---->:C dictArgs('kw',c = 'C',d = '12121212',kw = 'KW') # kw 1:kw # kw 2:B # the kw ---->:KW # the d ---->:12121212 # the c ---->:C
argc是命令行总的参数个数
argv[]是argc个参数,其中第0个参数是程序的全名,然后是命令行后面跟的用户输入的参数
>>>test.exe a.c b.c t.c
则argc = 4
argv[0] = "test"
argv[1] = "a.c"
argv[2] = "b.c"
argv[3] = "t.c"
举报