[Tutor] Optional parameter passing

wormwood_3 wormwood_3 at yahoo.com
Mon Jan 12 23:34:27 CET 2009

You know, that's a great idea :-)  Just using options I could keep my initial checks the same (e.g. making sure needed options were included by looking at options.foo) and pass it along without adding much to the query function.


Samuel Huckins

Homepage - http://samuelhuckins.com
Tech blog - http://dancingpenguinsoflight.com/
Photos - http://www.flickr.com/photos/samuelhuckins/
AIM - samushack | Gtalk - samushack | Skype - shuckins

From: Kent Johnson <kent37 at tds.net>
To: wormwood_3 <wormwood_3 at yahoo.com>
Cc: Python Tutorlist <tutor at python.org>
Sent: Monday, January 12, 2009 5:09:31 PM
Subject: Re: [Tutor] Optional parameter passing

On Mon, Jan 12, 2009 at 4:39 PM, wormwood_3 <wormwood_3 at yahoo.com> wrote:
> Hello all,
> I have used *args and **kwargs to have a function accept optional
> parameters, but is there a lazy way to optionally pass parameters? For
> example, say my script can accept a number of parameters for a database
> connection, such as user, password, and database name. The function that
> makes the MySQL call has a default for user, say "root". So if the user
> didn't pass in a value for the user option, I don't want to pass it to the
> function doing the MySQL call. I don't want to have to do:
> if options.user:
>     do_mysql_query(user)
> else:
>     do_mysql_query()
> and so on for each possible option. Is there a better way?

You would have to put the options into a collection, a list or dict or
class. You can use *args and **kwargs at the point of call as well as
in a function definition.

What about having the defaults in the options object and just passing
it to the function?

or pass the options and have the function use code like
  user = options.user or 'root'

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090112/314e14d2/attachment.htm>

More information about the Tutor mailing list