使用argv将参数传递给python中的函数 [英] passing arguments to functions in python using argv

查看:62
本文介绍了使用argv将参数传递给python中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请找到下面的脚本

from sys import argv
script, contact_name, contact_no = argv

def operation()
    some code goes here

自从我从命令行运行这个脚本后,我如何将 contact_name 和 contact_no 传递给操作函数?我正在使用 python 2.7

Since i run this script from command line, How do i pass contact_name and contact_no to operation function? I am using python 2.7

推荐答案

允许任何参数

from sys import argv

def operation(name, number, *args):
    print(name , number)

operation(*argv[1:])

从命令行调用:

python myscript.py Om 27

第一个和第二个 argv(Om"和 27 将分别传递给名称和数字)额外的 argv(如果有)将用于 *args 元组变量.

first and second argv ("Om" and 27 will pass to the name and number respectively) additional argv(if any) will to *args tupple variable.

这篇关于使用argv将参数传递给python中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆