How do comparison operators work? I thought they could be used only to compare numeric values, 5 <= 8, etc. But in this code sets are compared:
str = 'The quick Brow Fox'
alphabet = string.ascii_lowercase
alphaset = set(alphabet)
b = alphaset <= set(str.lower()) # Does it automatically extract length of objects?
print(len(alphaset)) # 26
print(len(set(str.lower()))) # 19
print(b)
26
15
False
I thought that it is impossible to do. alphaset <= set(str.lower())
, you know literally is, e. g. set() <= set()
. Does an operator implicitly call len()
on such objects to find some numeric values to compare?
How does it know that one sequence is bigger, less or equal etc. to another?
Python supports operator overloading, which means that any class can implement methods that provide access to the standard operators.
For full documentation of what you can do in Python, including which methods a class can implement to support different operators, check out the Python data model.
For a description of how a built-in type like set
implements its operators, see that type's documentation. For example, documentation for the set type.