"import" vs. "from .. import" in Python: Which uses more memory?

  Рет қаралды 8,428

Python and Pandas with Reuven Lerner

Python and Pandas with Reuven Lerner

Күн бұрын

Пікірлер: 24
@Ferret2YourFace
@Ferret2YourFace 4 жыл бұрын
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.
@walkdead94
@walkdead94 3 жыл бұрын
Man... that was a big mistake from my part! Thanks for the advice!
@andreasjanzen8446
@andreasjanzen8446 4 жыл бұрын
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.
@ReuvenLerner
@ReuvenLerner 4 жыл бұрын
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".
@shyamprasad7017
@shyamprasad7017 4 жыл бұрын
Nice Explanation.
@zacky7862
@zacky7862 3 жыл бұрын
This is great explanation! Thanks for this tutorial
@poordelir
@poordelir 2 жыл бұрын
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.
@ReuvenLerner
@ReuvenLerner 2 жыл бұрын
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
@ThipuVaasan
@ThipuVaasan 2 жыл бұрын
What if you have multiple files in your mymod and importing first file using "from" does also imports other files?
@ReuvenLerner
@ReuvenLerner 2 жыл бұрын
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!)
@peschebichsu
@peschebichsu 3 жыл бұрын
Very interesting, thank you!
@sherzodbekesanov2655
@sherzodbekesanov2655 3 жыл бұрын
This is very useful information, thanks a lot man ; ) +1 like and subscriber
@vividvault9285
@vividvault9285 3 жыл бұрын
Take my subscription You'll be getting another watch from me.
@ReuvenLerner
@ReuvenLerner 3 жыл бұрын
Delighted to know you enjoyed!
@shyamprasad7017
@shyamprasad7017 4 жыл бұрын
From the above explanation, I guess using import package or from package import module uses same memory.
@ReuvenLerner
@ReuvenLerner 4 жыл бұрын
Yup!
@kjsk9510
@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
@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.
@MagnusAnand
@MagnusAnand 4 жыл бұрын
Great video!
@mistwire
@mistwire 4 жыл бұрын
I thought I knew the answer to that... I was wrong 🤣
@vbaskaran
@vbaskaran 4 жыл бұрын
Awesome
@srinithia9816
@srinithia9816 3 жыл бұрын
What's the difference between import And import*
@ReuvenLerner
@ReuvenLerner 3 жыл бұрын
See my video on that subject ("Foiling from .. import *")!
@SkyyySi
@SkyyySi 3 жыл бұрын
`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.
Python's bitwise operations: What they are, how they work, and how to use them
13:18
Python and Pandas with Reuven Lerner
Рет қаралды 19 М.
Understanding "with" and Python's context managers
14:00
Python and Pandas with Reuven Lerner
Рет қаралды 1 М.
Cool Parenting Gadget Against Mosquitos! 🦟👶 #gen
00:21
TheSoul Music Family
Рет қаралды 32 МЛН
MY HEIGHT vs MrBEAST CREW 🙈📏
00:22
Celine Dept
Рет қаралды 79 МЛН
the balloon deflated while it was flying #tiktok
00:19
Анастасия Тарасова
Рет қаралды 10 МЛН
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 447 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Method chaining in Pandas
18:17
Python and Pandas with Reuven Lerner
Рет қаралды 2,2 М.
__new__ vs __init__ in Python
10:50
mCoding
Рет қаралды 209 М.
The Fastest Way to Loop in Python - An Unfortunate Truth
8:06
mCoding
Рет қаралды 1,4 МЛН
Metaclasses in Python
15:45
mCoding
Рет қаралды 155 М.
Python comments vs. docstrings: What, how, and why
7:05
Python and Pandas with Reuven Lerner
Рет қаралды 5 М.
Python dataclasses will save you HOURS, also featuring attrs
8:50
What does Python's __init__ method do?
6:56
Python and Pandas with Reuven Lerner
Рет қаралды 34 М.
Cool Parenting Gadget Against Mosquitos! 🦟👶 #gen
00:21
TheSoul Music Family
Рет қаралды 32 МЛН