You can inline objects of a custom class:
from functools import total_ordering
M = total_ordering(type('maxstring', (), {'__lt__': lambda *_: False}))()
print(sorted([M, 'abc', M, 'zyx']))
# ['abc', 'zyx', <__main__.maxstring object>, <__main__.maxstring object>]
Unfortunately, if you're going to inline this object more than once, and want to make sure that multiple maxstring objects compare equal, you should fix its comparison ==
operator:
M1 = total_ordering(type('maxstring', (), {'__lt__': lambda *_: False}))()
M2 = total_ordering(type('maxstring', (), {'__lt__': lambda *_: False}))()
print(M1 == M1)
# True
print(M1 == M2)
# False
M1 = total_ordering(type('maxstring', (), {'__lt__': lambda *_: False, '__eq__': lambda s,o: type(o).__name__=='maxstring'}))()
M2 = total_ordering(type('maxstring', (), {'__lt__': lambda *_: False, '__eq__': lambda s,o: type(o).__name__=='maxstring'}))()
print(M1 == M2)
# True
Important note: Do not make your maxstring an instance of str
, otherwise it will be treated half like a maxstring and half like an empty string, and this will mess up the comparisons:
M = total_ordering(type('maxstring', (str,), {'__lt__': lambda *_: False}))()
print(isinstance(M, str))
# True
print(sorted([M, 'abc', M, 'zyx']))
# ['', 'abc', '', 'zyx']
print((M < 'abc'), (M > 'abc'), ('abc' < M), ('abc' > M))
# False False False False
str.__gt__()
or you want to construct astring
that results to greater than any otherstring
? – Ewellstr
, and object would do, that implements the comparison operators accordingly. – Haughty