hashlib contains implementations for hash algorithms. Unfortunately two consecuting calls to hashlib.sha256() do not produce two different instances of SHA256 but the same one: If called twice hashlib.sha256() will return the same object each time. So apparently we have a singleton here
This is bad in a all situations where any kind of concurrency is required. Additionally this is very bad in situations where you want to provide a hashing object to some algorithm. In my case: Tests fail as the same object is reused instead of creating a new one.
My question: How an I create two instances of SHA256 (or any other hash algorithm)?
Example:
import hashlib
print(hashlib.sha256())
print(hashlib.sha256())
This will output something like:
<sha256 HASH object @ 0x7fb3611b4710>
<sha256 HASH object @ 0x7fb3611b4710>
<sha256 HASH object @ 0x101f9a260>objects ... can you provide the code you are using to determine this? I suspect you are misusingid(rather, falling into the all too common trap). Note, for mehashlib.sha256() is hashlib.sha256()returnsFalseid, i.e. in this example0x7fb3611b4710, because in CPython, as soon as the reference count reaches zero, the object is reclaimed. The object only exists long enought to be passed intoprint. The CPython runtime optimizes allocations, is is perfectly happy to reuse the memory from the last object.