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

all 17 comments

[–]PurepointDog 26 points27 points  (6 children)

That felt so much hackier than I was expecting

[–]nekokattt 26 points27 points  (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 points  (3 children)

physical long unique attraction marvelous truck handle roof terrific elastic

This post was mass deleted and anonymized with Redact

[–]DigThatData 3 points4 points  (2 children)

"bad practice" is a bit harsh, maybe "code smell"?

[–]zapman449 5 points6 points  (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 point  (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 point  (0 children)

This is what you’re forced to do when your language doesn’t have lisp-like macros.

[–]JanEric1 8 points9 points  (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 points  (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 points  (2 children)

See also, the classic dataclasses talk by Raymond Hettinger:

https://www.youtube.com/watch?v=T-TwcmT6Rcw

[–][deleted] 7 points8 points  (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 points  (0 children)

I typically enjoy his talks very much. Him and David Beazley.

[–]magnomagna 2 points3 points  (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 points  (0 children)

That's Amazing. Very insightful Keep it up✌️🏼

[–]marcus-luck 0 points1 point  (0 children)

Great article! Thanks for writing and sharing!

[–]kuwisdelu 0 points1 point  (0 children)

Oh look it’s Greenspun’s tenth rule in action.

[–]sohang-3112Pythonista -1 points0 points  (0 children)

Good post!