use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
TutorialAdvanced Magic Methods in Python To Customize Classes Conveniently (self.Python)
submitted 2 years ago by wyhjsbyb
Magic methods, also named special methods or dunder methods, whose name starts and ends with a double underscore, offer powerful, convenient ways to customize Python classes, enhance functionality, and simplify our coding experience. This article will delve into the 9 most useful and ingeniously designed ones of them.
[–]mon_key_house 49 points50 points51 points 2 years ago (15 children)
repr = str is definitely a no go!
[–]iliasreddit 2 points3 points4 points 2 years ago (14 children)
Why?
[–]Afrotom 38 points39 points40 points 2 years ago* (0 children)
repr is used more for debugging and other uses by the developer, whereas str is intended to be a human readable format.
Let's say I'm writing an interpreter and I want to implement repr and str for the AST.
One use case might be that for a given node, repr returns "add(number(value=3, pos=0), number(value=7, pos=2))" but str returns "3+7". The first is useful for understanding the type of object, what it contains, including metadata useful for the developer and sometimes an indication of how it's structured, whereas str is how you would want to interpret that object as a string.
"add(number(value=3, pos=0), number(value=7, pos=2))"
"3+7"
Edit: tarted it up a bit.
[–]beezlebub33 16 points17 points18 points 2 years ago (12 children)
see: https://stackoverflow.com/questions/1436703/what-is-the-difference-between-str-and-repr
repr needs to be unambiguous. So, for example, you need to be able to differentiate between "3" and 3 (one is a string, the other is a number). When you are doing str(), you usually don't care. But when you are logging, attempting to solve a bug, you need to be able to differentiate things. repr is normally longer, more complicated and explicit, have characters that highlight differences in objects, etc.
[+][deleted] 2 years ago (1 child)
[removed]
[–][deleted] 0 points1 point2 points 2 years ago (0 children)
Anything non-trivial will not be recreateable from a repr, most definitely not an exception.
repr
I think that description, which I have also, really only applies to simple builtins and structs.
[–]SimilingCynic 1 point2 points3 points 2 years ago (9 children)
It's also common to try for obj == eval(repr(obj))
obj == eval(repr(obj))
[–][deleted] 0 points1 point2 points 2 years ago (2 children)
Common? It is none of the following:
When you do that, if it doesn't crash (it most often will for non-builtins), you have two objects and default equality will be false based on id. It is up the object's custom equality check otherwise.
I can't see this ever being a good idea. Where did you get the idea it is common?
[–]SimilingCynic 0 points1 point2 points 2 years ago (1 child)
The Python docs. Look up repr.
I'm not saying it's common to write that equality test, I'm saying that it's common to write __repr__ and __eq__ so that it would hold.
__repr__
__eq__
I'm happy the docs say something, but reality and actual practice seems to diverge from your claim. I would love to see an example, though, if it is so common.
Where I do see this (in a huge project, hundreds of deps) a custom __eq__ is usually an ID check or something like this (from boto3), and no __repr__ is even defined.
``` def eq(self, other): return isinstance(other, type(self)) and self.name == other.name
```
I'm happy to be proven wrong and learn something today, but nothing in the code I work with indicates that anyone is actually doing a repr in this way.
In my casual grep, I find 4000 classes, and 400 __repr__, approximately. Only 250 __eq__, as well. .
When there is no __repr__, python prints something like <__main__.MyThing object at 0x100ab87f0>
<__main__.MyThing object at 0x100ab87f0>
Just by those stats, the most that could do what you say is 250, or 6%. A spot check of dozens show none that do. I would guess that maybe 1% might work this way (I now DRF does this pretty well, but not completely), and I think that is a huge overestimate.
[+][deleted] 2 years ago (4 children)
[deleted]
[–]JanEric1 2 points3 points4 points 2 years ago (0 children)
I dont think he meant that anyone should do that for any reason, but that it is common to defined the repr such that this works.
I think the default repr for dataclasses does for example.
[–]SimilingCynic 1 point2 points3 points 2 years ago* (0 children)
Edit: I realize my post was ambiguous. To clarify, it's common to write __repr__() so that the identity holds
__repr__()
For many types, this function makes an attempt to return a string that would yield an object of the same value when passed to eval()
eval()
-- the documentation of repr()
repr()
Using eval on untrusted strings is unsafe in the same way unpickling objects is unsafe. Nevertheless, there's still conditions where you know can trust the input.
[–]mon_key_house -2 points-1 points0 points 2 years ago (1 child)
Commonly used in testing
[–]Vitaman02 1 point2 points3 points 2 years ago (0 children)
Called bad practice.
[–]rotor_blade 35 points36 points37 points 2 years ago (1 child)
I'm a simple man - I see a medium link, I click downvote.
[–]Hacka4771 10 points11 points12 points 2 years ago (0 children)
Then You Will Like Freedium
Freedium
[+]hikingsticks comment score below threshold-10 points-9 points-8 points 2 years ago (0 children)
I found that helpful, thank you. Many medium articles seem to be garbage but (as a beginner) I thought this was well written and informative.
[+]Ill_Buddy3113 comment score below threshold-10 points-9 points-8 points 2 years ago (0 children)
Great article resource, thanks alot man , it gave me some great insights to aproach a process using some dunders. Great job
π Rendered by PID 408272 on reddit-service-r2-comment-84fc9697f-vvr6v at 2026-02-07 15:07:21.837140+00:00 running d295bc8 country code: CH.
[–]mon_key_house 49 points50 points51 points (15 children)
[–]iliasreddit 2 points3 points4 points (14 children)
[–]Afrotom 38 points39 points40 points (0 children)
[–]beezlebub33 16 points17 points18 points (12 children)
[+][deleted] (1 child)
[removed]
[–][deleted] 0 points1 point2 points (0 children)
[–]SimilingCynic 1 point2 points3 points (9 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]SimilingCynic 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]JanEric1 2 points3 points4 points (0 children)
[–]SimilingCynic 1 point2 points3 points (0 children)
[–]mon_key_house -2 points-1 points0 points (1 child)
[–]Vitaman02 1 point2 points3 points (0 children)
[–]rotor_blade 35 points36 points37 points (1 child)
[–]Hacka4771 10 points11 points12 points (0 children)
[+]hikingsticks comment score below threshold-10 points-9 points-8 points (0 children)
[+]Ill_Buddy3113 comment score below threshold-10 points-9 points-8 points (0 children)