diff options
author | Raymond Hettinger <python@rcn.com> | 2015-01-14 06:57:35 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2015-01-14 06:57:35 (GMT) |
commit | 1a8ada89f9b3d9b10654adce979046d865906a44 (patch) | |
tree | 27f1e32fe3c1ea003cddbce15d748f0553585c3c /Lib/functools.py | |
parent | e54dd0b92b00a47d88cd959399331a3fdf77d5d5 (diff) | |
download | cpython-1a8ada89f9b3d9b10654adce979046d865906a44.zip cpython-1a8ada89f9b3d9b10654adce979046d865906a44.tar.gz cpython-1a8ada89f9b3d9b10654adce979046d865906a44.tar.bz2 |
No need to rebuild a constant dictionary on every call. Move convert mapping to module level.
Diffstat (limited to 'Lib/functools.py')
-rw-r--r-- | Lib/functools.py | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/Lib/functools.py b/Lib/functools.py index 2ae8313..fa5bfde 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -174,28 +174,29 @@ def _lt_from_ge(self, other): return op_result return not op_result +_convert = { + '__lt__': [('__gt__', _gt_from_lt), + ('__le__', _le_from_lt), + ('__ge__', _ge_from_lt)], + '__le__': [('__ge__', _ge_from_le), + ('__lt__', _lt_from_le), + ('__gt__', _gt_from_le)], + '__gt__': [('__lt__', _lt_from_gt), + ('__ge__', _ge_from_gt), + ('__le__', _le_from_gt)], + '__ge__': [('__le__', _le_from_ge), + ('__gt__', _gt_from_ge), + ('__lt__', _lt_from_ge)] +} + def total_ordering(cls): """Class decorator that fills in missing ordering methods""" - convert = { - '__lt__': [('__gt__', _gt_from_lt), - ('__le__', _le_from_lt), - ('__ge__', _ge_from_lt)], - '__le__': [('__ge__', _ge_from_le), - ('__lt__', _lt_from_le), - ('__gt__', _gt_from_le)], - '__gt__': [('__lt__', _lt_from_gt), - ('__ge__', _ge_from_gt), - ('__le__', _le_from_gt)], - '__ge__': [('__le__', _le_from_ge), - ('__gt__', _gt_from_ge), - ('__lt__', _lt_from_ge)] - } # Find user-defined comparisons (not those inherited from object). - roots = [op for op in convert if getattr(cls, op, None) is not getattr(object, op, None)] + roots = [op for op in _convert if getattr(cls, op, None) is not getattr(object, op, None)] if not roots: raise ValueError('must define at least one ordering operation: < > <= >=') root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__ - for opname, opfunc in convert[root]: + for opname, opfunc in _convert[root]: if opname not in roots: opfunc.__name__ = opname setattr(cls, opname, opfunc) |