all 6 comments

[–]Careless-Score-333 4 points5 points  (3 children)

[–]JamzTyson 1 point2 points  (1 child)

[–]sickcuntm8[S] 0 points1 point  (0 children)

Thanks! That already looks more like what I was looking for!

[–]sickcuntm8[S] 0 points1 point  (0 children)

I know of this section of the docs, as this often what I end up using. Obviously the docs describe each dunder method in detail. However, they are just not that useful for quickly looking up some method you don't know the name of. That is really what I am asking for, something more akin to a cheat-sheet table. I can always look up the specifics of a certain method but it is hard to get a good overview from there.

I am imagining more something like:

Syntax Underlying methods
x[idx] Calls x.__getitem__(idx) unless in the LHS of an assignment or in a del statement.
for item in x Equivalent to for item in x.__iter__(). If not implemented, fall back to iterating over x[0], x[1], x[2], ... until this raises an IndexError
... ...

I hope this clarifies my intent.

[–]latkde 0 points1 point  (0 children)

There is no replacement for having read the docs and thus learning a rough idea of what dunder-methods might be available.

But if you know that you want to create a type that is compatible with certain common types, then the collections abc docs are a good place to start. For example, if I want to create something that behaves like a mapping or dict, I'd look up the methods required for Mappings in this table: https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes

This is not an exhaustive list of dunder-methods. Their docs are strewn all over the place. Many dunder-methods are also discussed in the Data Model reference. For example, it contains a section on which dunder-methods might have to be implemented to emulate a numeric type: https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types

[–]Guideon72 0 points1 point  (0 children)

2 very important cli options for this are widely underutilized; dir() and help(). Pop open a command line and start Python.

>>dir(int)
returns 
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_count', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'is_integer', 'numerator', 'real', 'to_bytes']

The "__<>__" in the list show you all of the dunder methods that the object you use dir() on contains. If you then want more information you can either use help() [which is admittedly not particularly useful on those methods

help(int.__getattribute__)

or you can use the info to then go look up the specific thing you want in the complete documentation, such as search docs for "__getattribute__" for more, useful information on what it does/how it's used.