On Wednesday, March 4, 2015 at 5:47:59 AM UTC-8, Simon King wrote:
>
> But wouldn't we still have the complication for Cython classes, that one 
> has to actually *copy* code from sage.structure.element.Element? If I 
> recall correctly, this is needed in order to create hashable elements 
> (if you override __hash__ then you must also override __cmp__). That 
> said, this could be resolved in the same way: We should *never* override 
> sage.structure.element.Element.__hash__. Instead, the generic __hash__ 
> method should call _hash_. 
>

There's a penalty to that. This _hash_ method would be an ordinary method 
lookup and hence MUCH slower than the special __hash__, which is lightning 
fast, because it's a slot method that gets special cased a lot in many 
situations as well.
For _cmp_ we're getting some benefit because the coercion framework needs 
to hook into it, but for __hash__ this doesn't hold.

If someone overrides __hash__ (which they should do because the default 
hash sucks badly) and wants to still inherit the original __richcmp__ (it 
makes little sense to rely on __cmp__ being a special method anymore) then 
they should do just that, rather than force the introduction of another 
hook.

On cython level, it's not unprecedented to hack your way to setting the 
appropriate slot methods if the standards provided by PyType_Ready (and 
cython) are not to your liking:

(from sage/structure/coerce_dict):

(<PyTypeObject *><void *>TripleDict).tp_traverse = &TripleDict_traverse
(<PyTypeObject *><void *>TripleDict).tp_clear = &TripleDict_clear

The only CPython C-API non-compliant bit about this is that we';re making 
the modification *after* PyType_Ready has been called on the type 
structure, and that is just because cython doesn't provide a hook to insert 
your own code before that.

For a python type exactly the same trick would work, because it would 
require changing the same slot.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to