1

I am trying to create a dictionary or list in python. The normal way for creating a dictionary is test = {} but I don't want to name the dictionary test. For example I want to name the dictionary a random hash that I generate.

bagel = "bagels" 
h = hashlib.md5()
h.update(bagel.encode('utf-8'))
print(h.hexdigest())

I then want to create the dictionary from the h.hexdigest variable.

h.hexdigest() = {}

But this will not create the dictionary with the hash I just generated.

Does anyone know how I can do this?

martineau
  • 119,623
  • 25
  • 170
  • 301
adoba yua
  • 41
  • 7
  • 3
    I don't understand the use-case. Why would you want to hash the name of an object? You could always create a dictionary with some hashed value as a key. – roganjosh May 18 '18 at 18:30
  • 1
    @Fortunato, sure that won't create a syntax error but it doesn't do what OP is asking for. It just makes `a` an empty dict. – pault May 18 '18 at 18:31
  • 1
    It's probably worth mentioning *why* you want to do this. For example, why not have an outer dictionary with a fixed name (like `dictofdicts`) and put your inner dictionary in that (like `dictofdicts[h.hexdigest()] = {}`)? – Arthur Tacca May 18 '18 at 18:32
  • 3
    The real question is why? What are you actually trying to do? Because I can guarantee with near certainty that if you even have to ask this question (it's not a bad question, just a bit beginner-y) then this probably isn't the right approach to what you are actually trying to accomplish. Note by the way that variable name spaces in Python are (roughly speaking) just dictionaries themselves so probably all you want is a dict with your hashes as keys. – Iguananaut May 18 '18 at 18:39
  • 3
    You don't want to create variables on-the-fly like that...how will you write code to reference the name since you won't know what it will be until runtime? Also see [Why you don't want to dynamically create variables](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html). – martineau May 18 '18 at 18:39
  • I believe this answers your question: https://stackoverflow.com/a/19122532/4162291 – Austin May 18 '18 at 18:42

2 Answers2

1

what you are asking might be possible if you could generate the random hash which does not start with a number. so, the following code will work only if the hex digit generated is doesnot start with a number

bagel = "bagels" 
h = hashlib.md5()
h.update(bagel.encode('utf-8'))
temp = h.hexdigest()
exec("{0} = dict()".format(temp))

so, to make it work for anything, i suggest you to add a constant string as shown below

bagel = "bagels" 
h = hashlib.md5()
h.update(bagel.encode('utf-8'))
temp = h.hexdigest()
exec("const_{0} = dict()".format(temp))
rawwar
  • 4,834
  • 9
  • 32
  • 57
  • Hi Thanks. I tried this hashed = random.getrandbits(128) chains = [] hashed = chains hashed.append("dexter") print(hashed) Does this mean the dictionary is now named after the generated hash? – adoba yua May 18 '18 at 18:44
  • its not working? – rawwar May 18 '18 at 18:44
  • It is working! And thanks but I am trying to see if I can do that as well for a different case! – adoba yua May 18 '18 at 18:45
  • different cases like what? – rawwar May 18 '18 at 18:46
  • I am trying to create a list, I want the name of the list to come from the generated hash. So first I create the list and call it list, I then generate the hash and name it hashed. I then say hashed = list, and then hashed.append("whatever"). But this doesn't name the list with the generated hash. – adoba yua May 18 '18 at 18:51
  • create a new question and explain clearly what you want so that i can help. please do provide an example in the post – rawwar May 18 '18 at 18:53
0

this might helpful for you.

import hashlib
bagel = "bagels"
h = hashlib.md5()
h.update(bagel.encode('utf-8'))
temp = h.hexdigest()

class YClass( object ):
    pass

y= YClass()
setattr( y, temp, {'greet': 'Hello'} )


print(y.__dict__)