The correct order for pep compliance is (see style guide 'Imports'): 1: Standard library imports. 2: Related third party imports. 3: Local application/library specific imports.
It's not __init__.py that makes a package. It's even not required anymore in Python 3.x. It's just having a folder. Basically, package = folder and module = file.
1) use module and package: module is individual python file, package is directory with many python modules; package requires __init__.py - which makes folder the package; whatever is in init, it will be run 2) one class in one file (may be exceptions) 3) group related functionality together 4) separate utility and helper functions 5) organize import statements
About organize imports, pep8 recommend use 1. standard library imports , 2. related third party imports, 3. local application/library specific imports . Anyway great video Thanks!
Thank you for creating this video on organizing Python code. It may be useful to others who want to improve the readability and maintainability of their code. Here are the 5 key points of the video. 1. Use modules and packages to better organize Python code. 2. Place each class in an individual file using Pascal case. 3. Group related functionality together in packages. 4. Place all utilities in a single file or package. 5. Organize import statements by third party, built-in, and local files.
It’s crazy how your comment sounds so much like chat gpt to me. I think I’ve been using gpt to much lately, because I’ve become paranoid that everything around me was written with the bot; when in reality, people are just having good grammar
Slowly I start to understand how GitHub Projects work. I must say as a "advanced" beginner who has just finished learning native Python it's pretty intimidating to see such big Projects and not understanding a single thing even though I'm not actually struggling with the Python part but I guess that's a thing you have to get used to :D Great video Tim, thank you very much ❤
I'm in the same boat. Tail end of my python course, which includes an "advanced python" section. What intimidates me is all the ways to potentially do 1 thing. Seeing the varying syntax freaks me out. What helped is getting organized, thinking in algorithms, and accepting that the web will answer all my syntax questions easily.
Great content, totally agree on pretty much everything. For Utils I would add a little thing: I usually recommend not to do it cause 1. junior dev would over use it and 2. It doesn’t help to know what’s inside. So for me, it is on very last option.
You can have the import statements auto sorted by using the isort tool, which formats the imports consistently. Available as a formatter in multiple LSPs.
I'd say if you have a hierarchy of classes (for example 2-3 classes that inherit from an abstract class) you should group them together in a single file, unless they're too large, but that probably means you are breaking a SRP rule
@@ThatOpinionIsWrongWhat would you say about class composition, then? If you have one class that is used strictly and extensively inside of a parent class, with no inheritance concerns involved, would it make sense to put the child class definition in the same file as its parent class definition?
Here's one little compliment you might not have got before. I do think your tutorials are among the very best on RUclips, but having seen some others, I am also very grateful you don't try to be cute or humorous or pumped with enthusiasm. I don't think humour comes easily to programmers (Socratica is an exception), and those who try it generally just sound lame. You give us the straight goods without making us squirm or groan. I really appreciate it.
I like to do built-ins first, then third party, then local libraries. Then I do initializations and globals, like input_dir = "". I guess what's important is having a system.
Though I know this, I still think these tips are important, and I know these not from courses but from reading coding books. Thanks, for me, these are great helps too.😁😁😁
I find this "separate helper functions" to be an antipattern. Functions that are helpful to one module should be in that module. Functions that are helpful in multiple places are a sign that some module should be doing that task for you already. By these tips you're making it harder to write proper code. Same with "unrelated things together", it's an antipattern of making a mess. Also, one file = one class sounds almost right but it promotes OOP style thinking, which in itself is kind of an antipattern. I'm not really sure what's the best way to go about it myself, but my own rule of thumb is to chop things up into modules and files to provide a single concept to user of that module or file. It's almost the same, except, a class isn't that good approximation of a concept. You'd be basically falling for OOP trap by forcing your concepts into class-shaped boxes.
@@lawrencedoliveiro9104 If the functions don't have clear domain where they operate in, it is to me the antipattern. If you can't name the common thing that all these parts of your program share, beside "helper", you're in a world of hurt if any of the parts coupled through "helper" change or want to be refactored, since there is no context for "helper" stuff to give you any expectations about which way things actually should be, why are things coupled, or even which things are coupled. It works if you get it right while you still had the entire program inside your head while making that. Anyone coming afterwards is going to be locked out of making any changes because aside from going through the program line by line to verify how the helpers are used, there's no way for you to know if any change is possible, reasonable, and what it will break. Contrast this with having helpers be tied to the module they're used in. You need to understand one module to make changes to helper functions. They're also now coming with context, since you don't need to name helpers, parameters, comments etc in a general non-specific manner, but rather you can have those helpers have very clear purpose in the context of the module. You making changes to helpers is now in terms of what makes sense in context, specifically for your module and the business logic surrounding it, and thus you also understand from changes what consequences it has. If you need same helper function in three different modules, I'd any day of the week that same helper function three times, once per module, and treat it as internal to each module.
@@gJonii Look at, for example, the built-in integer operations. Do they have a “clear domain where they operate in”? Or are they in fact widely used in a lot of different code?
@@lawrencedoliveiro9104 built-in integer operations? You have the domain in the name, it's integers. Them being used in projects is confusing developer of the built-in interface, and developers of python programs.
If I'm inside a folder, wanting to call functions or classes from another folder, same project, how do I do that? Does this structure only work with main files not inside folders?
Hey Tim, i just wanted to say that you didn't add the other videos to the intermediate python list Future topics: 9. Collections: orderedDict 10. Collections: defualtDict 11. docstrings These are missing lol Big fan
I wanted to do a class for file in one package but all of them need to import one file and when I try to import the classes from outside the package it gives an error cause of the import
I think "local imports last" should be a hard rule, in case you're overwriting built-in functions or classes you should never load built-in modules over it
I'd hate to break the 69 comments but, Tip one is a personal game changer that I should have learned in my years of full stack dev studies. I feel like I just learned a basic...
Tech With Tim I kept making a lot of projects on my portfolio about React, Python and APIs. I just got hired! My first job in tech and it’s ove $100K at an internet company. The thing is I won’t see any of the languages I worked hard to learn. If anything, at my job I will see some html and css and that’s it. Other than that, I’m looking at presentation for ads, review analytics with google analytics and make some calls to the employees working at the company’s stores about the updates. Don’t get me wrong. I’m happy I am making 117k but I sometimes think that if I don’t practice coding I might forget and who knows I might need it in the future. BTW I don’t want to go through interviews again. It was horrible many companies told me no and thank goodness this one told me yes because I was 2 months with no job down to my last $200 and finally I got in but I feel at the long run I might become lazy. What do you think? Do you think it’s probably good I don’t have to code too much?
i suggest try doing one leetcode question everyday or once in every 2-3 days because you have a job starting side project can be hectic also leetcode will help you in future if you ever thought of changing job
@@neuron8186 Right now I’m still on training but they already told us what I’ll be doing. That’s the thing, my job is multiple things. It’s more like graphic designing, analytics, see code but only html and css and call people that work at the stores. But my title says Analytical Software Developer. Again, I know I shouldn’t complain. Because from last year from June to December I had an internship as an engineer looking at React and cypress and one of the head of Engineers told me once that he’s always worried about deadlines because if anything happened before or during production the blame was on him. I really feel I shouldn’t complain because I didn’t work for 2 months it’s just that in my mind I always thought on building cool stuff. Again I don’t have stress at my job and feel happy for the pay but I sometimes think I have so much more potential
@@inspisiavideos8542 Always improve yourself; if anything, even if in terms of career, you aren't using what you learnt, you can always advance on your own personal terms. Make something massive, something impressive that you can call yours. Maybe in a new language. Hope you're still doing well at your job.
Cant you just put your imports in an import package called imports? then you have one import at the top if your main that calls all your imports? & they can be organized further in there.
I prefer to use relative import (from [dot]package import this) as it makes more clear we are importing a local package and not, maybe, a standard library's module with the same name
Relative imports can get messy though when you have to import files across your separate modules and have to pay attention to how many periods you write depending on how deep you are in your directory tree. Separating your standard library, third-party and local imports with newlines work just as well but keep the consistency of your code. Also it's bad practice to give your directories the same name as existing libraries, this is what directory naming conventions are for
There are a lot of things in pep8 that I disagree with. Why not name the file exactly as the classname? That way, if a file begins with a capital letter, you know it contains a class and you know exactly what that class is called. If a file begins with a lowercase letter, you know it's a script without a class. I use this convention for PHP code and it works great.
The "import" in "__init__" does not work as I've would expect it. If the statements in __init__ are run when we import the module, then I would expect that when we in `__init__` have `from forces import Forces` then we should be able to just used `Forces` right away in `main.py` and not have to import it first (since the "import" part is run in the "__init__"). So, just to make sure I've understood it correctly; import in `__init__` does not actually import, but "moves" the imports to the main-module such that they can be imported directly from the main-module and not the submodule.
I have outgrown the difficulty of what you teach but i still come back time to time because your content is what taught me most of what i know
Same
He’s literally the best and I hate to say literally
😭👍 same here
Me too
me three, its allways good to repeat the basics tho
The correct order for pep compliance is (see style guide 'Imports'):
1: Standard library imports.
2: Related third party imports.
3: Local application/library specific imports.
you are correct
I’ve been doing this for so long without knowing it was an actual named standard. It’s just logical
It's not __init__.py that makes a package. It's even not required anymore in Python 3.x. It's just having a folder. Basically, package = folder and module = file.
1) use module and package: module is individual python file, package is directory with many python modules; package requires __init__.py - which makes folder the package; whatever is in init, it will be run
2) one class in one file (may be exceptions)
3) group related functionality together
4) separate utility and helper functions
5) organize import statements
About organize imports, pep8 recommend use 1. standard library imports
, 2. related third party imports, 3. local application/library specific imports . Anyway great video Thanks!
And this can be achieved automatically with tools like isort or ruff (my linter of choice) + pre-commit git hooks
Thank you for creating this video on organizing Python code. It may be useful to others who want to improve the readability and maintainability of their code. Here are the 5 key points of the video.
1. Use modules and packages to better organize Python code.
2. Place each class in an individual file using Pascal case.
3. Group related functionality together in packages.
4. Place all utilities in a single file or package.
5. Organize import statements by third party, built-in, and local files.
It’s crazy how your comment sounds so much like chat gpt to me. I think I’ve been using gpt to much lately, because I’ve become paranoid that everything around me was written with the bot; when in reality, people are just having good grammar
@@live_destin-3408...
Wow, this is exactly what i was searching for when improving in coding but didn't learn it from scratch just somewhere in between. Thank you so much!
Slowly I start to understand how GitHub Projects work. I must say as a "advanced" beginner who has just finished learning native Python it's pretty intimidating to see such big Projects and not understanding a single thing even though I'm not actually struggling with the Python part but I guess that's a thing you have to get used to :D
Great video Tim, thank you very much ❤
I'm in the same boat. Tail end of my python course, which includes an "advanced python" section. What intimidates me is all the ways to potentially do 1 thing. Seeing the varying syntax freaks me out. What helped is getting organized, thinking in algorithms, and accepting that the web will answer all my syntax questions easily.
A useful video, it's one thing to be able to code in Python, but to manage your files is just as important.
This vid was exactly what i need to finish my project, thank you so much!
Every time i see a notification from your channel i feel so happy and excited to learn something new❤️
@♜ Pinned by Tech With Tim scam
I totally agree with all of your tips. Especially the No 2; which I also practice when I write C code... since 1990.
Feels like this guy knows everything I need to know in python
Just starting my journey here and I am enjoying this content from over the years. Great stuff Tim
OMG, THIS WAS ACTUALLY SO USEFUL. Thanks a lot, really learned a lot.
Great content, totally agree on pretty much everything.
For Utils I would add a little thing: I usually recommend not to do it cause 1. junior dev would over use it and 2. It doesn’t help to know what’s inside. So for me, it is on very last option.
Because of this guy, I learned how to organize my code 👌
You can have the import statements auto sorted by using the isort tool, which formats the imports consistently.
Available as a formatter in multiple LSPs.
One class per file makes your code very organized, I agree. However, if two classes are very related it is okay to put them together
I'd say if you have a hierarchy of classes (for example 2-3 classes that inherit from an abstract class) you should group them together in a single file, unless they're too large, but that probably means you are breaking a SRP rule
@@ThatOpinionIsWrongWhat would you say about class composition, then? If you have one class that is used strictly and extensively inside of a parent class, with no inheritance concerns involved, would it make sense to put the child class definition in the same file as its parent class definition?
Honestly if you can get away with one class per file you are not using OOP enough
You are the GOAT ❤
No words to explain the contents you made! Just Amazing.
Absolutely fantastic! Thank you :)
amazing tutorial, what a legend :D
Here's one little compliment you might not have got before. I do think your tutorials are among the very best on RUclips, but having seen some others, I am also very grateful you don't try to be cute or humorous or pumped with enthusiasm. I don't think humour comes easily to programmers (Socratica is an exception), and those who try it generally just sound lame. You give us the straight goods without making us squirm or groan. I really appreciate it.
I been trying to work this out for the last 3 days,.... you are a legend but it makes my head hurt,.. thank you though.
awesome content and is nicely presented. Thanks.
you post stuff as i search for it, why, ily so much
Thanks Tim 👍🌟
I like to do built-ins first, then third party, then local libraries. Then I do initializations and globals, like input_dir = "". I guess what's important is having a system.
I agree to keep classes in individual files.
Thank you Tim! God bless you!
Great video thanks so much! I have been using modules but not local packages thanks!
Though I know this, I still think these tips are important, and I know these not from courses but from reading coding books. Thanks, for me, these are great helps too.😁😁😁
Hi Tim! great video , i enjoyed it and now i feel like i can easily take on python
Excellent tips.. thanks!
I find this "separate helper functions" to be an antipattern. Functions that are helpful to one module should be in that module. Functions that are helpful in multiple places are a sign that some module should be doing that task for you already. By these tips you're making it harder to write proper code.
Same with "unrelated things together", it's an antipattern of making a mess.
Also, one file = one class sounds almost right but it promotes OOP style thinking, which in itself is kind of an antipattern. I'm not really sure what's the best way to go about it myself, but my own rule of thumb is to chop things up into modules and files to provide a single concept to user of that module or file. It's almost the same, except, a class isn't that good approximation of a concept. You'd be basically falling for OOP trap by forcing your concepts into class-shaped boxes.
Functions that are useful in multiple places are a sign of effective code reuse.
@@lawrencedoliveiro9104 If the functions don't have clear domain where they operate in, it is to me the antipattern. If you can't name the common thing that all these parts of your program share, beside "helper", you're in a world of hurt if any of the parts coupled through "helper" change or want to be refactored, since there is no context for "helper" stuff to give you any expectations about which way things actually should be, why are things coupled, or even which things are coupled.
It works if you get it right while you still had the entire program inside your head while making that. Anyone coming afterwards is going to be locked out of making any changes because aside from going through the program line by line to verify how the helpers are used, there's no way for you to know if any change is possible, reasonable, and what it will break.
Contrast this with having helpers be tied to the module they're used in. You need to understand one module to make changes to helper functions. They're also now coming with context, since you don't need to name helpers, parameters, comments etc in a general non-specific manner, but rather you can have those helpers have very clear purpose in the context of the module. You making changes to helpers is now in terms of what makes sense in context, specifically for your module and the business logic surrounding it, and thus you also understand from changes what consequences it has.
If you need same helper function in three different modules, I'd any day of the week that same helper function three times, once per module, and treat it as internal to each module.
@@gJonii Look at, for example, the built-in integer operations. Do they have a “clear domain where they operate in”? Or are they in fact widely used in a lot of different code?
@@lawrencedoliveiro9104 built-in integer operations? You have the domain in the name, it's integers. Them being used in projects is confusing developer of the built-in interface, and developers of python programs.
@@gJonii So you feel Python shouldn’t have such operations, because you find them “confusing”?
Great video, very useful. Thanks.
this is not related to your video but the cat in the intro was so cute :D
WASHHHHHHHHHHHHHHHHHHH SOPHIE IS SO CUTE! 😻😻😻
Nice video, Tim!
If I'm inside a folder, wanting to call functions or classes from another folder, same project, how do I do that? Does this structure only work with main files not inside folders?
Hey Tim, i just wanted to say that you didn't add the other videos to the intermediate python list
Future topics:
9. Collections: orderedDict
10. Collections: defualtDict
11. docstrings
These are missing lol
Big fan
Isn't dict ordered in recent python versions?
I wanted to do a class for file in one package but all of them need to import one file and when I try to import the classes from outside the package it gives an error cause of the import
I think "local imports last" should be a hard rule, in case you're overwriting built-in functions or classes you should never load built-in modules over it
GREAT VIDEO! btw small youtuber here!
Thank you very much. Very useful tips. Just subscribed to ProgrammingExpert.
thanks that was very helpful
THANK YOUUUUUUU! I am doing a Python project for college and this is exactly what I needed.
Its super niceee 🎉
what is this star in snowflake_time function definition?
I'd hate to break the 69 comments but,
Tip one is a personal game changer that I should have learned in my years of full stack dev studies. I feel like I just learned a basic...
Tech With Tim I kept making a lot of projects on my portfolio about React, Python and APIs. I just got hired! My first job in tech and it’s ove $100K at an internet company. The thing is I won’t see any of the languages I worked hard to learn. If anything, at my job I will see some html and css and that’s it. Other than that, I’m looking at presentation for ads, review analytics with google analytics and make some calls to the employees working at the company’s stores about the updates. Don’t get me wrong. I’m happy I am making 117k but I sometimes think that if I don’t practice coding I might forget and who knows I might need it in the future. BTW I don’t want to go through interviews again. It was horrible many companies told me no and thank goodness this one told me yes because I was 2 months with no job down to my last $200 and finally I got in but I feel at the long run I might become lazy. What do you think? Do you think it’s probably good I don’t have to code too much?
Definitely keep coding fresh in your head.
i suggest try doing one leetcode question everyday or once in every 2-3 days because you have a job starting side project can be hectic also leetcode will help you in future if you ever thought of changing job
@@neuron8186 Right now I’m still on training but they already told us what I’ll be doing. That’s the thing, my job is multiple things. It’s more like graphic designing, analytics, see code but only html and css and call people that work at the stores. But my title says Analytical Software Developer. Again, I know I shouldn’t complain. Because from last year from June to December I had an internship as an engineer looking at React and cypress and one of the head of Engineers told me once that he’s always worried about deadlines because if anything happened before or during production the blame was on him. I really feel I shouldn’t complain because I didn’t work for 2 months it’s just that in my mind I always thought on building cool stuff. Again I don’t have stress at my job and feel happy for the pay but I sometimes think I have so much more potential
@@inspisiavideos8542 Always improve yourself; if anything, even if in terms of career, you aren't using what you learnt, you can always advance on your own personal terms. Make something massive, something impressive that you can call yours. Maybe in a new language. Hope you're still doing well at your job.
great tips. but would be great to do more on point #1 - Use Modules and Packages.
Thanks 👍
is ok to define all required function in single python file ?
Cant you just put your imports in an import package called imports? then you have one import at the top if your main that calls all your imports? & they can be organized further in there.
Hey tim , please make video how to read programming document??
how would this change with pygame? imports & such seem more complicated, also pygame.init() seems to need to be in each module, etc. Any tips?
I prefer to use relative import (from [dot]package import this) as it makes more clear we are importing a local package and not, maybe, a standard library's module with the same name
Relative imports can get messy though when you have to import files across your separate modules and have to pay attention to how many periods you write depending on how deep you are in your directory tree. Separating your standard library, third-party and local imports with newlines work just as well but keep the consistency of your code. Also it's bad practice to give your directories the same name as existing libraries, this is what directory naming conventions are for
Thanks!
So theoretically, malicious code can be be injected into __init__.py and do anything, like, say, append itself to all __init__.py on the host?
Sophie out there! 🤦♂️😅
@♜ Pinned by Tech With Tim ridiculous!
What VS Code theme do you use?
needed this
Can you take a course for computer olympiad topics?🙏
Hi Tim, your videos are super helpful! Can I ask a question please: how can you enable execution time in vs code? Thanks!
You can use time module in python and print the difference between end time and start time.
Hey, I was just searching for something like this. What are the chances?
wow look at the cat on the intro :D
Hey Tim, will you 💞 know me which software you are using for video 📷 editing Please...
we came Sophie, we stayed for the tips
Your just legend
There are a lot of things in pep8 that I disagree with. Why not name the file exactly as the classname? That way, if a file begins with a capital letter, you know it contains a class and you know exactly what that class is called. If a file begins with a lowercase letter, you know it's a script without a class. I use this convention for PHP code and it works great.
How do you name files that both are standalone scripts and also contain classes you import elsewhere?
@@samwilson5544 personally I wouldn't have that. Those would be separate files. Too hard to find things if a file has too many different things in it.
I need to learn to structure code properly .
Hey Tim 👍
Love this
thank you
great videos
The "import" in "__init__" does not work as I've would expect it.
If the statements in __init__ are run when we import the module, then I would expect that when we in `__init__` have `from forces import Forces` then we should be able to just used `Forces` right away in `main.py` and not have to import it first (since the "import" part is run in the "__init__").
So, just to make sure I've understood it correctly; import in `__init__` does not actually import, but "moves" the imports to the main-module such that they can be imported directly from the main-module and not the submodule.
sophie the cat is adorable
Thanks
Could you make a video about cat role in the programmer's life?
My import order is based on the length of the line, and in descending order
Ooh interesting, I usually do it in ascending, and also with all "import" lines coming before all "from import ..." lines...
Can you make one for golang? That would be helpful 😃
Do I have to import a module (such as Pygame) into each module/package __init__ file that I create myself?
After tip 2 I was expecting tip 3 to be "switch to coding in Eclipse"
Sophie is cute
@Tech with Tim how many cats do you have?
Does Sophie code too 😆?
Yes she’s my assistant
0:10 What breed is she?! Too precious.
how to import packages that is not on the sane directory? I've tested relative path but it's not working
Nice cat sophia
I saw the cat 🐱😳
Allah razı olsun
I give you like for Sophie🥰😄...
I been doing it all wrong I only knew about camel case,... 😢
TIM CAN MAKE Django Tutorials
whats peppe et?
i am more focused on the cat
Single class = single file isn't controversial... convention is from S.O.L.I.D..
Tim can make more DJANGO tutorials?
Second
@♜ Pinned by Tech With Tim dont worry be ddos