CKTIS / Python
object.__le__
Методът определя по-малко или равно и може да изразява частична наредба.
Версия: Python 3.14; repository image, production runtime not attested
Синтаксис
left <= rightМетодът определя по-малко или равно и може да изразява частична наредба.
object.__le__
Python не го извежда от < и == и не създава < от него. Отразеният __ge__ може да поеме сравнението с предимство за строг подклас отдясно. Два отказа водят до TypeError.
Включването на права дава True True False, а < се проваля.
class Left:
def __le__(self,other): return NotImplemented
class Right:
def __ge__(self,other): return 'reflected'
assert (Left()<=Right())=='reflected'
try: Left()<=object()
except TypeError: pass
else: raise AssertionError
Примери
class Rights:
def __init__(self, names): self.names = set(names)
def __le__(self, other):
if not isinstance(other, Rights): return NotImplemented
return self.names <= other.names
a, b = Rights(["read"]), Rights(["read","write"])
print(a <= a, a <= b, b <= a)
try: a < b
except TypeError as exc: print(type(exc).__name__)
Резултат
True True False TypeError
Включването на права дава True True False, а < се проваля.