CKTIS / Python
object.__le__
O método define menor ou igual e pode exprimir uma ordem parcial.
Versão: Python 3.14; repository image, production runtime not attested
Sintaxe
left <= rightO método define menor ou igual e pode exprimir uma ordem parcial.
object.__le__
Python não o deduz de < e == nem gera < a partir dele. O reflexo __ge__ pode intervir com prioridade para uma subclasse estrita à direita. Duas recusas dão TypeError.
A inclusão de direitos dá True True False e < falha.
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
Exemplos
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__)
Resultado
True True False TypeError
A inclusão de direitos dá True True False e < falha.