oren oren - 1 month ago 14
Python Question

pymssql connect function

i have this function
pymssql.connect(host="my host",user="my user",password="my pass",database="mydb")

I want to read the user and password from the user and put them there is that possible or named arguments lvalue should not be variable and if yes then how could i do that ?

i.e is it possible to call this function like:
pymssql,connect(host=varaible,...)

Answer

Your question is worded... strangely. Are you having trouble with setting default arguments in a function definition?

>>> 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

If that wasn't what you were looking for, did you want to prompt the user for a missing argument?

>>> 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

Using a sentinel value of None for a missing argument is the idiomatic way in Python to detect a missing argument and execute another function.

Comments