In fact, I remembered that dict was not hashable, but by reading the doc I 
saw "dict.__hash__", so I was thinking that the problem came not from this; 
actually, this was just a normal point.
So thank for your explanation; finally I did a new class based on tuple but 
with some dict-behaviors, I think it was the simpler since Dimension is a 
very simple object.

Le vendredi 27 avril 2012 05:01:00 UTC+2, smichr a écrit :
>
> Here's a frozendict with reference: 
>
> ``` 
> class frozendict(dict): 
>     # from http://code.activestate.com/recipes/414283-frozen-dictionaries/ 
>     def _blocked_attribute(obj): 
>         raise AttributeError("A frozendict cannot be modified.") 
>     _blocked_attribute = property(_blocked_attribute) 
>
>     __delitem__ = __setitem__ = clear = _blocked_attribute 
>     pop = popitem = setdefault = update = _blocked_attribute 
>
>     def __new__(cls, *args, **kw): 
>         new = dict.__new__(cls) 
>
>         args_ = [] 
>         for arg in args: 
>             if isinstance(arg, dict): 
>                 arg = copy.copy(arg) 
>                 for k, v in arg.items(): 
>                     if isinstance(v, dict): 
>                         arg[k] = frozendict(v) 
>                     elif isinstance(v, list): 
>                         v_ = list() 
>                         for elm in v: 
>                             if isinstance(elm, dict): 
>                                 v_.append( frozendict(elm) ) 
>                             else: 
>                                 v_.append( elm ) 
>                         arg[k] = tuple(v_) 
>                 args_.append( arg ) 
>             else: 
>                 args_.append( arg ) 
>
>         dict.__init__(new, *args_, **kw) 
>         return new 
>
>     def __init__(self, *args, **kw): 
>         pass 
>
>     def __hash__(self): 
>         try: 
>             return self._cached_hash 
>         except AttributeError: 
>             h = self._cached_hash = hash(frozenset(self.items())) 
>             return h 
>
>     def __repr__(self): 
>         return "frozendict(%s)" % dict.__repr__(self) 
> ``` 
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sympy/-/OcVwCRXkaUMJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to