From: Artem Y. <ne...@gm...> - 2008-08-25 22:54:39
|
Yuri Takhteyev wrote: >> def getRandomToken(n=10, seed=RND_SEED): >> random.seed(seed) >> return "".join([chr(random.randint(97, 122)) for i in range(n)]) >> > > Personally, I tend to use a static string with all the letters in > cases like this just to save the reader from having to remember or > guess what characters are between chr(97) to chr(122). Sometimes a > "dumber" solution is just easier to understand. Eitherway, there > should be no magic numbers in the code, so if we want to use chr() > then the proper thing to do would be to define constants > > LOWERCASE_ASCII_A = 97 > LOWERCASE_ASCII_Z =122 > > and then later: > > return "".join([chr(random.randint(LOWERCASE_ASCII_A, > LOWERCASE_ASCII_Z)) for i in range(n)]) > > And then suddenly "abcdefghijklmnopqrstuvwxyz"[random.randint(1,26)] > doesn't look so verbose. :) > > - yuri > > Yes, in terms of readability, your variant looks better, let's decide in favour of it. |