1

I'm working with a python package (MySQLdb). The connect method has lots of positional parameters, most of which have defaults, but some aren't easy to deduce.

How can I only specify the parameters I want?

i.e. if a connect method has the following signature:

connect(username, password, socket, timeout)

and socket has a default value which may be system-dependent

is it possible to invoke it with something like the following so I don't overwrite the default value for socket:

connect('tom', 'passwd12', , 3)

3 Answers 3

5

Try this:

connect(username='tom', password='passwd12', timeout=3)

For more information please see Using Optional and Named Arguments.

Sign up to request clarification or add additional context in comments.

4 Comments

mysql-python.sourceforge.net/MySQLdb.html#mysqldb seems to suggest that most are positional, not keyword params though
@petraus: if you read down that page you'll see exact same code as posted in the answers.
thanks for the link. must have missed that when i read the book.
@SilentGhost - oh yeah, i didn't see that :-)
2

It's better to use keyword arguments rather than positional parameters in this case. As is obvious

connect ('tom', 'passwd12, None, 3)

is a lot less understandable than

connect (username = 'tom',
         password = 'passwd12',
         timeout = 3)

Comments

1

the MySQLdb-python source says this about connect:

"It is strongly recommended that you only use keyword parameters."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.