Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / Python

object.__le__

Il metodo definisce minore o uguale e può esprimere un ordine parziale.

Versione: Python 3.14; repository image, production runtime not attested

Sintassi

left <= right

Il metodo definisce minore o uguale e può esprimere un ordine parziale.

object.__le__

Python non lo deduce da < e == e non ne genera <. Il riflesso __ge__ può intervenire con priorità per una sottoclasse stretta a destra. Due rifiuti danno TypeError.

L’inclusione dei diritti dà True True False e < fallisce.

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

Esempi

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__)

Risultato

True True False
TypeError

L’inclusione dei diritti dà True True False e < fallisce.

Fonti