What is the __gt__ method in Python?
The __gt__ method defines the > operator for your classes. See reflected comparisons, NotImplemented, total_ordering and dataclass ordering.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
The __gt__ method defines the > operator for your classes. See reflected comparisons, NotImplemented, total_ordering and dataclass ordering.
Python 3 derives != from __eq__ automatically. See when a custom __ne__ is needed, how to write it safely, and how dataclasses remove the boilerplate.
__eq__ defines what == means for your objects. Learn how to implement it correctly, keep __hash__ consistent, and let dataclasses generate it for you.
__xor__ defines how your objects respond to the ^ operator. Learn the reflected and in-place variants, NotImplemented and practical XOR uses.
__or__ implements the | operator for your classes. See how reflected and in-place variants work, and when to use enum.Flag instead of hand-written flags.
In Python, and is a short-circuiting keyword, not a method. The special method __and__ implements the bitwise & operator for custom classes.
pow() in Python computes powers and, with a third argument, fast modular exponentiation. Learn the differences from ** and math.pow, plus modular inverses.
Python has no mod method. Learn how the % operator, divmod(), math.fmod and the __mod__ special method compute remainders.
The __floordiv__ method defines what the // operator does for your own classes. See reflected and in-place variants, NotImplemented and floor semantics.
__truediv__ makes the / operator work on your own Python classes. See how NotImplemented, reflected and in-place division work, with tested examples.
Learn how __mul__ lets your Python classes support the * operator, and why __rmul__ and NotImplemented matter.
The __sub__ method lets your Python classes support the minus operator. See how it works with NotImplemented, __rsub__, __isub__ and practical examples.
__contains__ is the special method behind the in operator. See how to implement it for custom classes, what Python does without it, and pitfalls to avoid.
len() returns the number of items in a Python object by calling its __len__ method. See built-in behaviour, custom classes and common pitfalls.
__delitem__ makes del obj[key] work on your own classes. Learn the signature, slice support, validation and how it fits with __getitem__ and __setitem__.
The __setitem__ method defines how an object responds to item assignment such as obj[key] = value. See examples for validation, slices and UserDict.
The __getitem__ method makes square-bracket access work on your classes. See integer, slice, string and tuple keys, plus errors and iteration.
delattr(obj, name) removes an attribute from an object using a string name. Learn how it differs from del, what it raises, and how properties and slots affect it.
Learn how Python setattr() sets object attributes dynamically, how it differs from dot assignment, common errors, and safe patterns with hasattr and allowlists.
getattr() reads an object attribute from a string name and can return a default if it is missing. See dynamic access, dispatch and safety tips.