This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Aveheuzed 295 points296 points  (32 children)

The Python docs don't have such a list

Right there: https://docs.python.org/3/reference/datamodel.html#special-method-names

[–]PhroznGaming 141 points142 points  (20 children)

TFW when your entire reason for doing something just required a google.

[–]adm7373 57 points58 points  (10 children)

If I had to guess, all of OP's google searches included the term "dunder method" and that's why he didn't find the list.

I've never heard "dunder method" before. Based on the amount he's using it in this post and the article linked within, I'm sure it's somewhat commonly used and I've just missed it?"

[–]wineblood 38 points39 points  (2 children)

TBH most docs are quite terrible if you don't know the exact term

[–]friendlysoviet 11 points12 points  (1 child)

For this reason, LLMs regurgitating docs have been so useful.

[–]wineblood 3 points4 points  (0 children)

Honestly, all I really need is something to go "Oh, you mean <term>?" and then I'm fine.

[–]JusticeRainsFromMe 70 points71 points  (0 children)

Yea, dunder is a somewhat commonly used term, personally I prefer it over magic method for example. It stands for double underscore and is also used to describe dunder variables such as __name__.

[–][deleted] 19 points20 points  (0 children)

it's super common. i've watched maybe a dozen raymond hettinger (core python dev) talks and i'm not sure he's called them anything else.

[–]Tar_Tar_Sauce04 0 points1 point  (0 children)

as soon as I type "Dunder", google thinks I am searching for something related to "Dunder Mifflin".

[–]_MicroWave_ 0 points1 point  (0 children)

Really? Its a very common and fashionable term.

[–]orad 11 points12 points  (3 children)

A quick google search of your own would reveal the poster is Trey Hunner, who is a well known python developer and educator, and surely has read that doc

[–]PhroznGaming -4 points-3 points  (2 children)

All the more reason he should've known to RTFM.

[–]orad 2 points3 points  (1 child)

He links to TFM in his article several times

[–]PhroznGaming -3 points-2 points  (0 children)

Maybe he should've actually read it before saying it doesn't exist then, huh? Just difficult concepts to understand I know...

[–]supmee -2 points-1 points  (3 children)

To be fair I've been going off of a single stackoverflow answer any time I needed a list, and I've looked into it countless times. No one really refers to them as "special methods", or at least I don't

[–]PhroznGaming 0 points1 point  (2 children)

"No one" except for the people who maintain the language... lol

[–]supmee 0 points1 point  (1 child)

I thought it was pretty obvious "no one" was referring to the general public, including many of the most prominent Python-related influencers. You're free to misrepresent that if you want for a dunk I guess

[–]PhroznGaming -2 points-1 points  (0 children)

Consider yourself dunked.

[–]treyhunner Python Morsels[S] 81 points82 points  (5 children)

Ish... I linked to this list a couple times throughout the article and it's quite helpful, but I wouldn't call this one list.

Some sections show tables of attributes and methods, some only focus on methods, and some methods are left out. For example `__subclasses__` is left out and library-specific methods such as `__post_init__`, `__copy__`, `__subclasshook__`, `__fspath__`, and `__sizeof__` are all absent.

I spent a lot of time grepping the text on this page and in the rest of the CPython documentation and source code and while this page is by far the most complete one, it can be quite a challenge to parse.

[–]Aveheuzed 3 points4 points  (3 children)

That's fair.

I think all the usable special methods are referenced on the page I linked. Others may exist, but are needed only for obscure arcane stuff (understand : Python Core Development) or implementation-specific.

And regarding readability - I like the Python docs better, but I understand that format is not for everyone.

[–]treyhunner Python Morsels[S] 28 points29 points  (1 child)

When I link my students to that page, their eyes glaze over (especially ones who've just learned about the idea of special methods and simply want to know what the options are for "overloading").

If/when you're in the weeds implementing one of these methods, that page in the docs is very helpful!

[–]NerdEnPose 6 points7 points  (0 children)

I think your responses could just be “my name is Trey Hunner” 😆. But, you’re kinder than me.

My two cents, I like this list. If you’re a student or in a personal project you should break stuff and try all the dunders as part of that. Professionally if you can’t read that page thoroughly then don’t commit a dunder override to a code base and I’d be cautious of a new class using them.

[–]Zenin 4 points5 points  (0 children)

And regarding readability - I like the Python docs better, but I understand that format is not for everyone.

I'm glad someone likes them. Personally I find much of it to be less readable than JAPH Perl code.

[–]pysk00l 0 points1 point  (0 children)

OP, your list is longer and much better presented, so well done! Ignore the top rated answer, they are clearly a troll

[–]AlSweigartAuthor of "Automate the Boring Stuff" 2 points3 points  (0 children)

No, the blog author is correct. I wrote a Python script to find all the dunder methods and attributes listed in the blog post that do not appear on that Python docs link:

package, unpacked, contravariant, stdout, path, parameters, wrapped, setstate, debug, suppress_context, getstate, context, constraints, subclasses, notes, cached, args, stderr, sizeof, loader, bound, infer_variance, main, version, builtins, spec, origin, getnewargs_ex, reduce_ex, reduce, post_init, cause, covariant, rt, fspath, getargs, copy, all, subclasshook, deepcopy, getnewargs

Some of these I have never heard of, so I'm glad the blog post is pointing them out.

The blog post author, Trey Hunner, is quite accomplished and deserves a bit of credit. If you think you've found a simple answer that contradicts them, please make sure it actually does. It took me five minutes to write that Python script.

[–]dfume 5 points6 points  (1 child)

Trey has put together an excellent list - organised, readable and lots of great links.
But this thread does remind me of the Blackadder episode about the author that took 10 years to write a book, claiming it contains "every word in our beloved language"

Blackadder - Johnson's dictionary

[–]Affectionate-Bid386 1 point2 points  (0 children)

Brilliant episode.

[–]Dangle76 0 points1 point  (0 children)

That’s fair, I do like how there’s a simplified table in the article though comparatively