python - Why is using 'eval' a bad practice? - Stack Overflow
I feel that this is just much more extensible than writing out an if/else block. However, I have heard that eval is unsafe. Is it? What is the risk? How can I solve the underlying problem in my class (setting attributes of self dynamically) without incurring that risk? Yes, using eval is a bad practice. Just to name a few reasons: In your case you can use setattr instead: There are some cases where you have to use eval or exec. But they are rare. Using eval in your case is a bad practice for sure. I'm emphasizing on bad practice because eval and exec are frequently used in the wrong place. Replying to the comments: It looks like some disagree that eval is 'very dangerous and insecure' in the OP case. That might be true for this specific case but not in general. The question was general and the reasons I listed are true for the general case as well. Using eval is weak, not a clearly bad practice. It violates the "Fundamental Principle of Software". Your source is not the sum total of wh
python - Why is using 'eval' a bad practice? - Stack Overflow New: Stack Overflow For Agents. The next generation of knowledge exchange. Learn more Collectives™ on Stack Overflow Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives Stack Internal Knowledge at work Bring the best of human thought and AI automation together at your work. Explore Stack Internal Why is using 'eval' a bad practice? Ask Question Asked 16 years, 6 months ago Modified 1 year, 11 months ago Viewed 92k times 198 I use the following class to easily store dat
Explore this link on the map →saved by
related reading
- Python's exec(): Execute Dynamically Generated Code – Real Pythonrealpython.com
- Demystifying evals for AI agents \ Anthropicanthropic.com
- Your Evals Will Break and You Won't See It Coming - Lun Wangwanglun1996.github.io
- [2408.02565] Reasons to Doubt the Impact of AI Risk Evaluationsarxiv.org
- Model evals for dangerous capabilities — LessWronglesswrong.com
- A starter guide for evals — AI Alignment Forumalignmentforum.org
- A starter guide for evals — LessWronglesswrong.com
- Gemini 3 is Evaluation-Paranoid and Contaminated — LessWronglesswrong.com
- styleguide | Style guides for Google-originated open-source projectsgoogle.github.io
- The bitter lesson of LLM evalsparsed.com
- Chapter 3: LLM Evaluations - ARENAlearn.arena.education
- 2312.06942arxiv.org