Thanks for the explanation. I create a lot of projects on my Raspberry Pi and I was looking for anything that could improve the performance when starting a program. This discovery is very disappointing but I am very thankful you showed me that this is no longer an angle worth exploring when optimizing.
@walkdead943 жыл бұрын
Man... that was a big mistake from my part! Thanks for the advice!
@andreasjanzen84464 жыл бұрын
Hi Reuven! Very nice video, thank you! With then Zen of Python ("Explicit is better than implicit") in mind, wouldn't it be better to use "import random" and "random.randint(...)" for the sake of clarity as to where the randint function is defined? Using "from random import randint" saves some typing, but is not as clear, particularly when you use modules that do not belong to the standard library.
@ReuvenLerner4 жыл бұрын
Well, "random" is definitely in the standard library! (It's not builtin, though.) As for the "from... import" vs. "import" issue, it seems to be a matter of taste. Using the full name is longer and clearer, I agree. But "from...import" is pretty common, especially for modules in the standard library like "random". I'd say that the more esoteric and unused the module, the less reasonable it is to use "from... import".
@shyamprasad70174 жыл бұрын
Nice Explanation.
@zacky78623 жыл бұрын
This is great explanation! Thanks for this tutorial
@poordelir2 жыл бұрын
Thank you for this very insightful video. Still I am struggling to understand the difference between: import datetime import datetime.timedelta from datetime import timedelta and why only the 3rd one works?? same for the similar modules.
@ReuvenLerner2 жыл бұрын
You have to distinguish between importing a module into memory and the variable that you define. "import datetime" loads the datetime module, and defines a global variable that refers to that module. "from datetime import timedelta" means that you want to import the datetime module, but then define a global variable referring to the timedelta attribute in that module. Take a look at my talk from PyCon Israel 2022 for more details: kzbin.info/www/bejne/eaPEf6N_ib-qiNE
@ThipuVaasan2 жыл бұрын
What if you have multiple files in your mymod and importing first file using "from" does also imports other files?
@ReuvenLerner2 жыл бұрын
If you say "import mymod" and mymod then has "import" lines, then all of the mentioned modules will be loaded into memory. The same is true with "from .. .import", either as the first command or in the modules you load. So in theory, "import mymod" can load 1,000 other modules into memory. (I sure hope not, though!)
@peschebichsu3 жыл бұрын
Very interesting, thank you!
@sherzodbekesanov26553 жыл бұрын
This is very useful information, thanks a lot man ; ) +1 like and subscriber
@vividvault92853 жыл бұрын
Take my subscription You'll be getting another watch from me.
@ReuvenLerner3 жыл бұрын
Delighted to know you enjoyed!
@shyamprasad70174 жыл бұрын
From the above explanation, I guess using import package or from package import module uses same memory.
@ReuvenLerner4 жыл бұрын
Yup!
@kjsk9510 Жыл бұрын
hmm.. but even if it is "from mymod import hello", Python is going from the Top to bottom. The fact that we are not seeing variables in the o/p is that they are not in the print(), but doesnot mean that Python is ignoring them.. Any thoughts on this? We should test out when we do "from mymod import hello" but try to print the mymod.x using print(mymod.x) then if we are seeing the o/p that means Python is reading all the variables and other parts of the code even if we try to import only one function.
@ReuvenLerner Жыл бұрын
When you use "from .. import", the module is executed from top to bottom. (Assuming, of course, that it hasn't been imported already.) The module object is stored in sys.modules; even if you don't see it, I promise you that it's there! Only the variable(s) you explicitly ask to be imported into the current namespace are defined.
@MagnusAnand4 жыл бұрын
Great video!
@mistwire4 жыл бұрын
I thought I knew the answer to that... I was wrong 🤣
@vbaskaran4 жыл бұрын
Awesome
@srinithia98163 жыл бұрын
What's the difference between import And import*
@ReuvenLerner3 жыл бұрын
See my video on that subject ("Foiling from .. import *")!
@SkyyySi3 жыл бұрын
`import ...` creates a class and puts everything in it. `from ... import *` doesn't do that. It sets al variables "directly". This means that it will probably clutter your variable scope by putting temporary variables there. It could also end up overwriting stuff.