A very personal note. Adding. Divide when it gets longer.
main
The name assigned to \ _ \ _ name \ _ \ _ when executed as a script
sample.py
def main():
print 'This function only run when that is main.'
if __name__ == '__main__':
main()
eq ne lt le gt ge
It means a function called Extended comparison.
It is owned by all objects and is also called by the comparison operator.
x> y andx.__gt__ (y)mean the same operation.
All six of these must be defined to define how objects are compared, In most cases, it is defined using a predefined comparison operation, so By total_ordering, one of [lt, le, gt, ge] and a total of two eq If you define The rest will be done automatically.
Quote: python documentation
total_ordering
@total_ordering
class Student:
def __eq__(self, other):
return ((self.lastname.lower(), self.firstname.lower()) ==
(other.lastname.lower(), other.firstname.lower()))
def __lt__(self, other):
return ((self.lastname.lower(), self.firstname.lower()) <
(other.lastname.lower(), other.firstname.lower()))
You use == in the definition of def __eq__ (self, other), that is, __eq__
Because <, that is, __lt __ is used in the definition statement ofdef __lt__ (self, other)
The rest will do the same.
Recommended Posts