RGDA1 for RDFlib by jpmccu · Pull Request #441 · RDFLib/rdflib (original) (raw)
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hash_cache={}
is problematic in this case due to the way python handles default values. In fact, the default values are created once, when the function is constructed, and reused for every call. For mutable values, this results in effects like the following:
>>> def f(i, s={}):
... s[i] = 'foo'
... return s
...
>>> print(f(0))
{0: 'foo'}
>>> print(f(1))
{0: 'foo', 1: 'foo'}
>>> print(f(2))
{0: 'foo', 1: 'foo', 2: 'foo'}
You probably want to do something like this:
def __init__(self, nodes, hashfunc, color=(), hash_cache=None):
if hash_cache is None:
hash_cache = {}
color=()
on the other hand is okay, because tuples are immutable.