Znalostní bázeCentrum zkoušek
CSČeština
Vybrat jazyk13

CKTIS / Python

object.__le__

Metoda definuje menší nebo rovno a může vyjádřit částečné uspořádání.

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

Syntaxe

left <= right

Metoda definuje menší nebo rovno a může vyjádřit částečné uspořádání.

object.__le__

Python ji neodvodí z < a == ani z ní nevytvoří <. Porovnání může převzít zrcadlové __ge__ s předností vlastní pravé podtřídy. Dvě odmítnutí způsobí TypeError.

Inkluze práv dává True True False a < selže.

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

Příklady

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

Výsledek

True True False
TypeError

Inkluze práv dává True True False a < selže.

Zdroje