CKTIS / Python
object.__le__
Metoda določa manjše ali enako in lahko izrazi delno urejenost.
Različica: Python 3.14; repository image, production runtime not attested
Sintaksa
left <= rightMetoda določa manjše ali enako in lahko izrazi delno urejenost.
object.__le__
Python je ne izpelje iz < in == ter iz nje ne ustvari <. Primerjavo lahko prevzame zrcalna __ge__ s prednostjo pravega podrazreda na desni. Dve zavrnitvi povzročita TypeError.
Vsebovanost pravic da True True False in < ne uspe.
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
Primeri
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
Vsebovanost pravic da True True False in < ne uspe.