Exec() VS Eval() Explained In Python Tutorial 2023

preview_player
Показать описание
What is the difference between exec() and eval() in Python, and how can we use them in our scripts? Well I'm here to teach you all of that today!

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Intro
00:15 eval()
01:34 Building a full calculator
02:29 exec()
03:59 Be careful
04:14 exec() does not return
04:39 Major side effect
06:08 Conclusion
Рекомендации по теме
Комментарии
Автор

One very niche use case that I use for exec() is to create variables dynamically

HitBoxMaster
Автор

they say "eval is evil" (in any programming language) and that's 100% true; because sooner or later you will evaluate some user input or remote "source"; and then ... poof, you're screwed; so it's better to forget about eval function and do it different from beginning, it's probably sometimes harder but in the long run it's better!

AlexCernat
Автор

You can definitely use exec() for malware particularly excecuting code from a remote source. And if done well is pretty difficult to identify

SlackOps
Автор

Thanks for the explanation, really good examples. Good to see that you sometimes type like us mortals. ☺

castlecodersltd
Автор

I ironically was using this last night to create a "remote" module loader (You can have a module in a gist, for instance, and load it into a project)

AlyceIsFree
Автор

exec allows to define globals see code:

print("set \"_\" to anything you want")
globals = {}
while True:
code = input("Code: ")
exec(code, globals)
value = globals.get("_")
print(f"the value of \"_\" is {value}")

jeremynaumann
Автор

Fun fact.. one of the reasons pickle is an unsafe serialization method, is because you can inject arbitrary python code using the exec() callable.. For example, one that sends all your AWS credentials in ~/.aws to the hacker :-)

sorvex
Автор

There are very very few use cases for either of these that make them worth the security holes they introduce in your code.

beef
Автор

interesting, this will be useful for converting c++ structs into dataclasses when sent over tcp.

chudchadanstud
Автор

So exec kinda inlines your script in a way? I'm getting some weird swift / c++ inlining vibes and would explain why the variables become available in whatever namespace you run it from

deathdogg
Автор

Actually in python, using the eval() function the 'calculator' can be written in one line: print(eval(input())). the problem with eval() is its limitations, so if you wanna write it the right way, you gotta handle some exceptions.

iskarik
Автор

I still don't get why I would run exec() on code instead of just running the code

enormousearl
Автор

he was really gonna print "jello, world"

mayo-neighs
Автор

Since everyone will tell you not to use these - why are they an option?

murphygreen
Автор

I always wonder why you write code like this with those : str hints. What is the benefit of "source: str " over simply calling it source?

elevendarter
Автор

You didn't explain the difference

RedHair
Автор

these type hints have got to go. I mean this python ppl. get your duck on.

DrDeuteron
join shbcf.ru