Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'example code creates a dictionary of class instances with random text keys'
- from random import choices, randint
- import string
- class Example():
- def __init__(self):
- self.num = randint(1, 100)
- def unique_key(size, instances):
- while True:
- key = ''.join(choices(string.ascii_lowercase +
- string.digits, k = size))
- if key not in instances:
- return key
- size = 10 # number of instances I require
- instances = {}
- for _ in range(size):
- instances[unique_key(size, instances)] = Example()
- for name, instance in instances.items():
- print(f'{name}: random number: {instance.num}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement