Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / Python

object.__le__

Metóda definuje menšie alebo rovné a môže vyjadriť čiastočné usporiadanie.

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

Syntax

left <= right

Metóda definuje menšie alebo rovné a môže vyjadriť čiastočné usporiadanie.

object.__le__

Python ju neodvodí z < a == ani z nej nevytvorí <. Porovnanie môže prevziať zrkadlové __ge__ s prednosťou vlastnej pravej podtriedy. Dve odmietnutia spôsobia TypeError.

Inklúzia práv dá True True False a < zlyhá.

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

Prí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ýsledok

True True False
TypeError

Inklúzia práv dá True True False a < zlyhá.

Zdroje