Putting logic in the __init__.py file sounds like an excellent opportunity for a "wtf is my code doing"-moment in a later debug session. Constants could be nice.
@2MinutesPy
Ай бұрын
Could be and sometimes it can be helpful
@LooksGoodMusic
28 күн бұрын
Surely implicitly, silently performing side effects when a module is imported is not a good idea
@blakephillips8649
Ай бұрын
Notice that he imported functions and not whole modules. Be careful when including whole modules since that can cause "circular import" errors. It's probably best practice not to import whole modules, but wanted to throw that nugget of personal experience out.
@exmachina767
Ай бұрын
But isn’t it the case that to be able to import a function from a module, the whole module must be read?
@FastKnight401
Ай бұрын
no, this doesn't fix the problem, you have to load the module first to then import the function. The thing is, if you import module, but not the functions, you may not actually get the import cycle error if it's a calltime dependency. The import cycle actually becomes a problem when you try to import a function from a partially initialized module, where the function isn't defined at the time you tried to import it, that's where you get an error. You shouldn't re-export functions in __init__.py files, because every single module in a package first imports that __init__.py file before being imported. Re-exporting functions will just cause every single module in your package require every other module in the package, which is the cause of subtle import loops. Your modules should depend on the __init__.py file, not the other way around.
@user-do1eg2kt3v
Ай бұрын
@@FastKnight401 Ohhh Thanks, that makes mores sense, i was hahing that exact problem
@dynamicgrad3820
28 күн бұрын
@@FastKnight401 i do not understand, could you explain with more details or share any thread on stackoverflow od Smith more?
@TheTmLev
Ай бұрын
Never ever ever put side effects and global variables to your __init__.py file
@FastKnight401
Ай бұрын
Something to be wary of is that every single module in a package has an implicit dependency on the __init__.py file, so re-exporting modules actually creates a fake dependency where every module in the package requires every other module in the package, leaving you with very subtle import loops. Because of this, I don't recommend re-exporting modules in __init__.py files at all. The modules should depend on the __init__.py files, not the other way around.
@2MinutesPy
Ай бұрын
Importing modules within init.py file means there is no need to explicitly import them into another code base after you imported the whole package. These modules are instantly ready to use as soon as the package is imported into another code or file
@FastKnight401
Ай бұрын
@@2MinutesPy the problem is the fact that those modules inside a package actually require the __init__.py of the package by default, even if you don't explicitly import it. By re-exporting those modules, what you're doing is that every single module inside that package requires every other module in the package, which can lead to very subtle import loops. I can only imagine how frustrating it is to deal with these types of import loops. This happens because the __init__.py module imports all the modules, and the modules require the __init__,py file to run first. This is why the module should be depending on the __init__.py file, not the other way around. If you still want the shorter names, you can make another fake package and import the modules in the fake package's __init__.py file. You'll probably have the real package named "_" while the fake package named "". I don't recommend re-exporting functions and stuff in the real package's __init__.py files to avoid the import loop problem. You may be able to debug import loop issues, but it could be restricting, and it's really subtle if you don't understand that modules implicitly import the __init__.py file of their package.
@dynamicgrad3820
28 күн бұрын
please, let me make sure od i understand correct, If I import pandas in the __init__.py file, is the pandas library available in all modules on the package?
@FastKnight401
27 күн бұрын
@@dynamicgrad3820 not really. So, say we have a structure: package_a __init__ module_a module_b and you do from package.module_a import what will happens is that the __init__ file will run first, then the module_a file will be imported. The stuff inside the __init__ file will not be accessible to the module_a file unless module_a explicitly imports package_a. The main problem is the implicit import of the __init__ file from the module_a file. If the __init__ file re-exports the functions in module_a and module_b, then importing module_a will always import module_b, even if module_b doesn't depend on it. This is because the __init__ file is run before import module_a, and the __init__ file imports module_a and module_b. The same way, importing module_b will always import module_a. This design decision is the reason why re-exporting modules may cause import loops. I could give an example of this if you want one, it's really tricky to debug.
@dynamicgrad3820
27 күн бұрын
@@FastKnight401 thank you! It would be really helpful, if you could provide aby example
@spidey2468
Ай бұрын
Now, another thing I can flex to my teammates of Junior Devs ;) Thanks
@2MinutesPy
Ай бұрын
Have fun!
@oussaber
Ай бұрын
Hey, fantastic animations! Which tool did you use to create them?
@vaitesh
Ай бұрын
Many thanks!
@2MinutesPy
Ай бұрын
You're welcome!
@fortnort7350
Ай бұрын
Isn't there also a thing where you check if the __name__ == "__main__" to see if something is being run as a program or a module?
@2MinutesPy
Ай бұрын
Yeah there is
@sachinpal5823
Ай бұрын
Nice one
@2MinutesPy
Ай бұрын
Thanks 🔥
@KieraTelecky-e7l
Күн бұрын
Tiffany Extensions
@franlopez7742
Ай бұрын
Credentials and tokens too, but only if you are a true member of the python gang
@2MinutesPy
Ай бұрын
Yeah using env variables
@N3krodamus
Ай бұрын
I am new in python, a year more or less, I used to program in perl. Anyway, I like the idea of logging modeule in init. Any advice about that? Sorry my english, si not my native lang. Thanks for the video.
@2MinutesPy
Ай бұрын
No problem. You can monitor your package using logging to log key events.
@GrzegorzZygan
Ай бұрын
For impatient. Nothing.
@MathsSciencesTech
14 күн бұрын
2MinutesPy making a Pi-minutes long video 🤔
@2MinutesPy
13 күн бұрын
😁
@abdullahsaid4765
Ай бұрын
Nice good tips thx u so much❤
@2MinutesPy
Ай бұрын
No prob
@FactFinders-of3mu
Ай бұрын
U r smart
@2MinutesPy
Ай бұрын
Thanks for appreciation
@nelsonlee1713
24 күн бұрын
The fact that this video has a length of 3.14
@2MinutesPy
24 күн бұрын
🌟
@trevoro.9731
Ай бұрын
Delete a random file from system32 if you are using it on Windows.
@neckbro
Ай бұрын
2minPY ... don't become 3minPY pls / Remember around 2min is the attention span of your subscribers...
@2MinutesPy
Ай бұрын
Yeah, it won't happen again and thanks for support
@neckbro
Ай бұрын
@@2MinutesPy You are excused!
@Manojkumar__
Ай бұрын
😂😂😂
@omkarkhatavkar8563
Ай бұрын
I do not care for making 2mins to 3 mins if that worthy to get good content
@javosch
Ай бұрын
@2MinutesPy if this is the quality of your 3 minutes videos, it is totally worth it!!!
@OswaldShoo
Ай бұрын
Why 3 minutes?
@2MinutesPy
Ай бұрын
Because the list is endless 😅😅
@neckbro
Ай бұрын
🤣
@compositeboson123
Ай бұрын
👍 (normaly Id say a lot of stuff but my battery is all time low)
@2MinutesPy
Ай бұрын
Thanks
@acopier
Ай бұрын
Actually from Python 3.3 you don't need to use __init__.py file
@2MinutesPy
Ай бұрын
Yeah right. Init.py file can help you get rid of circular imports error within a package and there are many scenarios where it could be helpful
@pepelover69
Ай бұрын
this video isn't two minutes, very disappointed
@2MinutesPy
Ай бұрын
Apologies
@pepelover69
Ай бұрын
@@2MinutesPy false advertising these days smh
@BhekizweShongwe
Ай бұрын
Then make yours. There is nothing wrong with the video’s content. Fly a kite…
Пікірлер: 63