[Tutor] Suggestions for more efficient and optimized coding technique,

spir denis.spir at free.fr
Thu Jan 8 22:06:30 CET 2009


Le Thu, 8 Jan 2009 11:34:49 -0500,
"Michael Langford" <michael.langford at gmail.com> a écrit :

> Here is your algorithm made more pythonic. Notice the use of default
> parameters, doc strings, not abbreviated variable names,  unix C style
> capitolization (very subjective, but often the one found in python
> libs), the avoidance of control variables when possible, the use of
> ascii_lowercase instead of your own string and  the not calling
> functions main that aren't __main__.
> 
> #!/usr/bin/env python
> import string
> import random
> 
> def rand_string(length = 5):
>   """returns a random string of numbers"""
>   return ''.join(random.sample(string.ascii_lowercase,length))

Since you wish to use (not necessary) optional args, it may be worth defining

def rand_string(length = 5):
  """returns a random string of numbers"""
  return ''.join(random.sample(string.ascii_lowercase,length))

which also avoids reading string.ascii_lowercase in each loop

denis


More information about the Tutor mailing list