我有这个函数pymssql.connect(host="my host",user="my user",password="my pass",database="mydb")
我想从用户那里读取用户和密码,并将它们放在那里,如果可能,或者命名参数lvalue不应该是变量,如果是,那么我如何做到这一点呢?
也就是说,可以像这样调用这个函数: pymssql,connect(host=varaible,...)
发布于 2010-03-10 01:47:56
你的问题很有条理...奇怪的是。您在函数定义中使用setting default arguments有问题吗?
>>> def f(arg1="hello", arg2="goodbye"):
print "arg1 is", arg1
print "arg2 is", arg2
>>> f()
arg1 is hello
arg2 is goodbye
>>> f(arg2="two")
arg1 is hello
arg2 is two
>>> f(1,2)
arg1 is 1
arg2 is 2
>>> f(arg2="foo", arg1="bar")
arg1 is bar
arg2 is foo如果这不是您要查找的内容,您是否希望提示用户缺少参数?
>>> def g(arg=None):
if arg is None:
arg = raw_input("What is the argument?")
print "The argument was", arg
>>> g(123)
The argument was 123
>>> g()
What is the argument? foo bar
The argument was foo bar在Python中,为缺少的参数使用标记值None是检测缺少的参数并执行另一个函数的惯用方法。
发布于 2010-03-10 02:08:37
要读取来自用户的输入,可以使用raw_input。
要使用程序的命令行参数,您必须使用import sys并使用sys.argv
例如:
import sys
myvar = raw_input("please input a var") #prompts the user for a value
myvar2 = sys.argv[1] #gets the first argument to the program然后,可以使用命名参数,如下所示
myfun(named=myvar, named2=myvar2)https://stackoverflow.com/questions/2411203
复制相似问题