Be Careful When Using exec() or eval() in Python

preview_player
Показать описание
In this video, we learn why using exec or eval in Python is risky and when you should avoid it.

◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚

🌐 Social Media & Contact 🌐

Рекомендации по теме
Комментарии
Автор

Substituting strings into program code is always going to be fraught. This is true of exec() and eval() in Python, it’s true of ♯define in C, and so on and so on.

This is why Python has explicit introspection facilities like getattr()/setattr()/dir() -- these were specifically added to the language to reduce the need for exec()/eval(). Also the fact that function and class definitions are not declarations, but are actually assignment statements that take effect at run-time, helps.

Compare LISP, where eval and macro substitution operate on syntactic tokens, not random strings. Generating and manipulating dynamic program code works much better there.

lawrencedoliveiro
Автор

Oh, for whatever reason, I always thought that Python didn't have eval(), but I guess I was wrong. ^^

HoarfrostTrickle
Автор

"eval" sounds almost like "evil"! 😦
Is this some sort of *_forbidden_* function?

kotface
Автор

why didnt the value of variable say "MYSECRET_PASSWORD"? we made a new variable with the same name so should it be overwritten?

clippy
Автор

make your videos sound louder...
Thanks

gutibaba
Автор

In 18 years using Python profesionally (about million lines written), i never used exec or eval. ever.
could it be useful? yes. Is it tho? no.

movaxh
Автор

I'm not python user so I don't care with this->video

ronaldweasly
Автор

Actually have a question about this or something similar:
Is it possible to pass attributes and methods as arbitrary arguments.
eg: Python
in dt.datetime.today()
can I relpace 'datetime' or 'today()' with an argument after the period.
like: dt.arg.today(), dt.datetime.arg


with eval/exec it's possible but very unsafe
eval(f"dt.{arg}.today()")

chriskathumbi
welcome to shbcf.ru