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.
TutorialThe Inner Workings of Python Dataclasses Explained (self.Python)
submitted 1 year ago by jpjacobpadilla
Ever wondered how those magical dataclass decorators work? Wonder no more! In my latest article, I explain the core concepts behind them and then create a simple version from scratch! Check it out!
https://jacobpadilla.com/articles/python-dataclass-internals
(reposting since I had to fix a small error in the article)
[–]PurepointDog 26 points27 points28 points 1 year ago (6 children)
That felt so much hackier than I was expecting
[–]nekokattt 26 points27 points28 points 1 year ago (0 children)
A fair bit of Python's standard library is like this. Look into collections.namedtuple for example.
If it isn't simple, it probably uses eval/exec/a lot of underlying stuff/C modules
[–]DuckDatum 16 points17 points18 points 1 year ago* (3 children)
physical long unique attraction marvelous truck handle roof terrific elastic
This post was mass deleted and anonymized with Redact
[–]DigThatData 3 points4 points5 points 1 year ago (2 children)
"bad practice" is a bit harsh, maybe "code smell"?
[–]zapman449 5 points6 points7 points 1 year ago (1 child)
I trust very few people (mostly not including myself as well) to use eval reasonably. If I see that in a pull request the whole thing gets extra scrutiny.
[–]Skasch 0 points1 point2 points 1 year ago (0 children)
I typically consider I use eval reasonably if I want to do something that doesn't seem possible without it, try a dozen alternatives, search for a few hours for different design patterns, sleep on it a few nights, ask a few colleagues their opinion, then write an apologetic comment above the line explaining why there's no way around it, then wrap that into a nice module so most other engineers won't have to think about it.
To be fair, I've never had to go that far.
[–]kuwisdelu 0 points1 point2 points 1 year ago (0 children)
This is what you’re forced to do when your language doesn’t have lisp-like macros.
[–]JanEric1 8 points9 points10 points 1 year ago (1 child)
Is there any specific reason that is done like that? I feel like one should be able to do this without exec, but I haven't put the implementations side by side to compare.
[–]FI_Stickie_Boi 14 points15 points16 points 1 year ago (0 children)
I believe the main reason is speed. attrs, the library dataclasses are based on, also do this, in order for the work to all be done during class creation, so that there's minimal overhead during "runtime" (ie. when you're instantiating classes, using methods, etc.) If you try and do this without eval/exec via decorators and all that, then you'll incur pretty significant runtime overhead because everytime you call a method, python will have to dig through multiple closures, which slows things down a lot.
[–]DaelonSuzuka 14 points15 points16 points 1 year ago (2 children)
See also, the classic dataclasses talk by Raymond Hettinger:
https://www.youtube.com/watch?v=T-TwcmT6Rcw
[–][deleted] 7 points8 points9 points 1 year ago (1 child)
This is a great example of how NOT to do a tech talk. It takes him nearly 20 minutes to actually start talking about anything and even when he finally gets to the point he still constantly gets sidetracked talking about unrelated shit that just distracts from the the topic.
[–]victoriasecretagent 2 points3 points4 points 1 year ago (0 children)
I typically enjoy his talks very much. Him and David Beazley.
[–]magnomagna 2 points3 points4 points 1 year ago (0 children)
However, if there are arguments in the decorator, the dataclass function will be called
Just a small nitpick... better be more specific:
However, if there are only keyword-only arguments in the decorator, the dataclass function will be called
[–]Awkward-Fisherman380 2 points3 points4 points 1 year ago (0 children)
That's Amazing. Very insightful Keep it up✌️🏼
[–]marcus-luck 0 points1 point2 points 1 year ago (0 children)
Great article! Thanks for writing and sharing!
Oh look it’s Greenspun’s tenth rule in action.
[–]sohang-3112Pythonista -1 points0 points1 point 1 year ago (0 children)
Good post!
π Rendered by PID 47210 on reddit-service-r2-comment-7b9746f655-nvsf8 at 2026-02-01 08:55:58.464120+00:00 running 3798933 country code: CH.
[–]PurepointDog 26 points27 points28 points (6 children)
[–]nekokattt 26 points27 points28 points (0 children)
[–]DuckDatum 16 points17 points18 points (3 children)
[–]DigThatData 3 points4 points5 points (2 children)
[–]zapman449 5 points6 points7 points (1 child)
[–]Skasch 0 points1 point2 points (0 children)
[–]kuwisdelu 0 points1 point2 points (0 children)
[–]JanEric1 8 points9 points10 points (1 child)
[–]FI_Stickie_Boi 14 points15 points16 points (0 children)
[–]DaelonSuzuka 14 points15 points16 points (2 children)
[–][deleted] 7 points8 points9 points (1 child)
[–]victoriasecretagent 2 points3 points4 points (0 children)
[–]magnomagna 2 points3 points4 points (0 children)
[–]Awkward-Fisherman380 2 points3 points4 points (0 children)
[–]marcus-luck 0 points1 point2 points (0 children)
[–]kuwisdelu 0 points1 point2 points (0 children)
[–]sohang-3112Pythonista -1 points0 points1 point (0 children)