[Tutor] use the data None

Sander Sweers sander.sweers at gmail.com
Mon Jun 4 13:01:07 CEST 2012


On 4 June 2012 12:14, Tehn Yit Chin <tehn.yit.chin at gmail.com> wrote:
> The potential for the variable not to exists is when I am using the
> optparser module, and I want to check if a particular parameter was passed
> in or not. If the parameter was not passed in, then the variable would not
> exists. Eg

Optparse will always create the variable, it may contain None.

> If I call a python script is expecting a parameter "param1" but I failed to
> passed it via the command line, I would do the following check
>
> (options, args)=parser.parse_args()
> if options.param1 != None:
>          param1 = int(options.param1 )
> else:
>          param1 = 30
>
> By the way, please excuse the way I am coding, I come from a C background.

The important part of you code is missing. You should show how you
create the parser (with the options etc).

The optparse module can provide a default value, did you know this?
See the online documentation on [1]. It shows an example of this right
at the beginning.

Greets
Sander
[1] http://docs.python.org/library/optparse.html


More information about the Tutor mailing list