I’m using the following function (which I think is pretty straight-forward) to generate a random string:
import sys
import string
import random
def random(size=16):
lst = [random.choice(string.ascii_letters + string.digits) for n in xrange(size)]
str = "".join(lst)
return str
However, I keep getting the following error:
AttributeError: 'function' object has no attribute 'choice'
Can someone tell me what’s wrong with my code? Google doesn’t seem to help, and I’m too new to Python to troubleshoot it effectively myself.
You are overriding the
import randomwith your function definition.The ‘random’ name in the Python namespace is now referring to you function not the
randommodule which you intended.Change the function name to something like
random_str