Python __Underscore__ Methods 1
Languages | Python |
Contents
- 1 Intro
- 2 Class and Instance Methods
- 2.1 Creation, Calling, and Destruction
- 2.2 Conversion to Strings
- 2.3 Truth Testing
- 2.4 Comparisons
- 2.4.1 "Rich Comparison" Operators
- 2.5 Logical and Mathematical Operators
- 2.5.1 Regular Binary Operations
- 2.5.2 Reversed Binary Operations
- 2.5.3 In-Place Binary Operations
- 2.5.4 Unary Operations
- 2.6 Casts
- 2.6.1 Real Numbers
- 2.6.1.1 Slice Indices
- 2.6.2 Complex Numbers
- 2.6.1 Real Numbers
- 2.7 Containing Items
- 2.7.1 Basics
- 2.7.2 Items and Slices
- 2.7.2.1 Items and New-Style Slices
- 2.7.2.2 Old-Style Slices
- 2.8 Attribute Access
- 2.9 Being Contained By Another Object
- 2.9.1 Descriptors
- 2.10 Pickling
- 2.10.1 Serializing and Unserializing Data
- 2.10.2 Customizing Object Creation upon Unpickling
- 2.10.3 Total Control
- 2.10.4 One More Thing
- 2.11 Copying
- 2.12 "With" Statements
- 2.13 Really Complicated
- 3 Class Methods
- 4 Class & Instance Properties
- 5 Class & Function Properties
- 6 File Properties
- 7 Builtin Functions
- 8 Module Properties
- 9 Modules
- 10 Other Things I've Seen Around
- 11 Talk about in Intro/Conclusion?
1 Intro
My intention for this article is to be a quick-reference guide: all the information you might need about a method or property condensed into a few lines. This might not be possible for something like __slots__, perhaps we should link to the Python Docs for details on the more complex ones. I know this stuff is documented in other places (for example, docs.python.org/ref/specialnames.html), but it's all over the place and hard to mentally parse. I want to teach while at the same time to be brief, if that is possible.
Anyways feel free to add, don't worry about messing stuff up, I can always revert your edits if I really hate them ; ) A list of underscore methods that I haven't written about yet is at the bottom; there might be more too that I don't know about. And things I'm not sure about have question marks next to them, feel free to search for question marks and correct any uncertainties.
2 Class and Instance Methods
For consistency's sake, let's say we have a class called 'Class', instances called 'x' and 'y'. Keeping with the Python docstrings, '<==>' can be read as 'is equivalent to'.
2.1 Creation, Calling, and Destruction
2.2 Conversion to Strings
2.3 Truth Testing
2.4 Comparisons
For instances to be meaningfully sorted, either __cmp__, __lt__, or __gt__ must be defined. If more than one is defined, __lt__ will be tried first, followed by __gt__ and then finally __cmp__.
2.4.1 "Rich Comparison" Operators
The following functions customarily return True or False, but can return any value. If in a boolean context, Python will call bool() on the returned value to determine truthfulness. NotImplemented should be raised if no valid comparison can be made.
If one of these methods is not defined, Python will try the opposite method with swapped arguments. For example, if you call x < other but x.__lt__ is not defined, Python will try the equivalent other.__gt__(x). If neither is not defined, x.__cmp__(other) will be tried, followed by other.__cmp__(x). If all four methods are undefined, instances are compared by object identity (address?).
Note that no other fallbacks will be performed. For example, if __le__ is not defined, Python does not call lt and eq. Similarly, Python will not fallback to __ne__ if __eq__ is not defined, or vice versa -- this can result in two instances that are 'equal' and 'not equal' at the same time.
There are no explicit 'reflected' (swapped-argument) versions of the comparison operators.
2.5 Logical and Mathematical Operators
2.5.1 Regular Binary Operations
These methods implment the binary operators +, -, , /, //, %, *, <<, >>, &, |, and ^, and the functions divmod and pow.
The method may raise NotImplemented if no operation is possible with the supplied with the supplied argument(s).
If not defined or if returns NotImplemented, and the two instances belong to different classes, Python will try to call the equivalent reflected binary operator method of the other instance, with this instance as the argument.
If that method is also not defined, the relevant operation is not possible and a TypeError ('unsupported operand type') is raised.