petraus
petraus

Reputation: 225

How to not specify default parameters in python?

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)

Upvotes: 1

Views: 163

Answers (3)

Andrew Hare
Andrew Hare

Reputation: 351486

Try this:

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

For more information please see Using Optional and Named Arguments.

Upvotes: 5

Dan D.
Dan D.

Reputation: 74645

the MySQLdb-python source says this about connect:

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

Upvotes: 1

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72745

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)

Upvotes: 2

Related Questions