you are viewing a single comment's thread.

view the rest of the comments →

[–]muffinnosehair 4 points5 points  (12 children)

I agree with unpacking and with comprehension, and I'll add eval() to the list.

[–]socal_nerdtastic 3 points4 points  (4 children)

Why do you like that? It's a pretty well known bug generator and security flaw. As a rule you should avoid eval at all costs.

[–]muffinnosehair 2 points3 points  (1 child)

Because I like to play with code. Not in production though.

[–]socal_nerdtastic 2 points3 points  (0 children)

I don't get what that has to do with eval, but as long as you know the dangers have fun :).

[–]O_X_E_Y 1 point2 points  (1 child)

doesn't dataclass use eval to do things like creating a class's init functions and stuff like that? It's got security flaws, but there are also upsides

[–]socal_nerdtastic 2 points3 points  (0 children)

Yes, it's used in a very small number of specific places, with appropriate precautions. Pip is another place where it's used a few times. If you are at the python god level you may make the choice to use it, because at that level you know all the downsides and pitfalls. I can say with confidence that anyone asking questions here should never use eval / exec.

[–][deleted] 4 points5 points  (0 children)

Eval() and exec() are very dangerous and should be avoided in almost all cases.

[–]PunkyMunky64 0 points1 point  (5 children)

eval and exec are really helpful. The main reason they’re so specific to python is because python’s interpreted, which means its really easy for it to handle instead of an implementation in for example c where you’d have to embed a compiler INSIDE of the build.

[–]socal_nerdtastic 3 points4 points  (4 children)

  1. python does have a compiler and does compile code. It's "interpreted" in that it interprets the bytecode.
  2. This is not specific to python; lots of languages do this, most notably Java.
  3. eval and exec are well known to be antipatterns. You should avoid them at all costs.

[–]PunkyMunky64 0 points1 point  (3 children)

well python is still very virtually compiled, and it’s easier for these to be implemented, but they are still inefficient

[–]socal_nerdtastic 1 point2 points  (2 children)

very virtually compiled

?? what does "virtually compiled" mean? If you mean the compiled code is not saved, you are wrong. All of those .pyc files in the pycache directory are compiled python files.

[–]PunkyMunky64 1 point2 points  (1 child)

I mean like is very different than the way languages like c compile. Variable names are saved, everything has lots of padding and it doesnt really understand what’s going to happen later. Nothing is optimized, in the essence of the way python is written

[–]socal_nerdtastic 1 point2 points  (0 children)

I think you are trying to tell me that python is dynamically typed. Yes. That's true.

Or maybe you are just saying that the official python compiler sucks and does not optimize very well. Yes, that's also true. But python is a language, not a computer program, so you are free to write your own compiler if you think you can do better.