Baza znanjaCentar za ispite
HRHrvatski
Odaberi jezik13

CKTIS / Python

object.__le__

Metoda definira manje ili jednako i može izraziti parcijalni poredak.

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

Sintaksa

left <= right

Metoda definira manje ili jednako i može izraziti parcijalni poredak.

object.__le__

Python je ne izvodi iz < i == niti iz nje stvara <. Usporedbu može preuzeti zrcalna __ge__ s prednošću prave podklase zdesna. Dva odbijanja uzrokuju TypeError.

Uključenost prava daje True True False i < ne uspijeva.

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

Primjeri

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

Rezultat

True True False
TypeError

Uključenost prava daje True True False i < ne uspijeva.

Izvori