While I can't really tell what you need this for, I am going to guess that you don't actually want global variables, just a single Person object that has a random/dynamic set of attributes. For that, you want setattr
and random.sample()
:
crazy_person = Person()
selected_attribute_names = random.sample(possible_attributes, 3)
for attribute_name in selected_attribute_names:
setattr(crazy_person, attribute_name, True)
# optional -- if you want to have a list of attributes, just copy that in:
crazy_person.attributes = selected_attribute_names
# vars(crazy_person).keys() will give you more or less the same thing though, for free
# hasattr(crazy_person, 'small') will tell you if you picked 'small'
# if we chose 'small', then from here on, crazy_person.small will be True as well