Learing Python, Newbie question

Greg Ewing greg at cosc.canterbury.ac.nz
Thu Oct 25 21:39:52 EDT 2001


Johannes Gamperl wrote:
> 
> if db_first:
>   db_flag = "w";
> else:
>   db_flag = "a";
> 
> is there a better way to write this?

If by "better" you mean "more readable and easier to
maintain", I would say no.

If you mean "shorter", "cuter", "more cryptic", etc.
you could use

  db_flag = "wa"[db_first]

provided db_first is known to be either 0 or 1.

(By the way, get out of the habit of putting semicolons on
the ends of statements in Python. They're redundant, and
your right little finger will thank you for it!)

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list