#12601: @cached_method does not work for special methods
-------------------------------------------------+-------------------------
Reporter: saraedum | Owner: jason
Type: enhancement | Status:
Priority: minor | needs_review
Component: misc | Milestone: sage-5.13
Keywords: cached_method, cache, | Resolution:
operator, special method | Merged in:
Authors: Simon King | Reviewers:
Report Upstream: Completely fixed; Fix | Work issues:
reported upstream | Commit:
Branch: | Stopgaps:
Dependencies: #15038 |
-------------------------------------------------+-------------------------
Comment (by SimonKing):
Replying to [comment:16 nbruin]:
> Example:
>
> {{{
> special_methods=set(['__get__','__set__','__hash__'])
>
> def is_special1(method):
> return method in special_methods
> def is_special2(method):
> return method.startswith('__') and method.endswith('__')
> }}}
With the full list and with Cython, one gets
{{{
sage: cython("""
....: cdef list special_methods = ['__abs__', '__add__', '__and__',
'__call__', '__cmp__', '__coerce__', '__contains__', '__del__',
'__delattr__', '__delete__', '__delitem__', '__delslice__', '__div__',
'__eq__', '__float__', '__floordiv__', '__ge__', '__get__', '__getattr__',
'__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__',
'__hex__', '__iadd__', '__iand__', '__idiv__', '__ifloordiv__',
'__ilshift__', '__imod__', '__imul__', '__index__', '__init__', '__int__',
'__invert__', '__ior__', '__ipow__', '__irshift__', '__isub__',
'__iter__', '__itruediv__', '__ixor__', '__le__', '__len__', '__long__',
'__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__',
'__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__',
'__radd__', '__rand__', '__rdiv__', '__repr__', '__rfloordiv__',
'__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__',
'__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__',
'__set__', '__setattr__', '__setitem__', '__setslice__', '__str__',
'__sub__', '__truediv__', '__xor__', 'next']
....: def is_special1(str method):
....: return method in special_methods
....: def is_special2(str method):
....: return method.startswith('__') and method.endswith('__')
....: """
....: )
....:
sage: timeit("is_special1('__hash__')",number=100000)
100000 loops, best of 3: 835 ns per loop
sage: timeit("is_special2('__hash__')",number=100000)
100000 loops, best of 3: 161 ns per loop
}}}
So, I disagree. If Cython is used then `startswith` and `endswith` are
faster.
--
Ticket URL: <http://trac.sagemath.org/ticket/12601#comment:18>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/groups/opt_out.