Generate password in python

前端 未结 9 1969
既然无缘
既然无缘 2020-12-12 21:51

I\'dl like to generate some alphanumeric passwords in python. Some possible ways are:

import string
from random import sample, choice
chars = string.letters          


        
9条回答
  •  一整个雨季
    2020-12-12 22:19

    WARNING this answer should be ignored due to critical security issues!

    Option #2 seems quite reasonable except you could add a couple of improvements:

    ''.join(choice(chars) for _ in range(length))          # in py2k use xrange
    

    _ is a conventional "I don't care what is in there" variable. And you don't need list comprehension there, generator expression works just fine for str.join. It is also not clear what "slow" means, if it is the only correct way.

提交回复
热议问题