5 Tips To Organize Python Code

5 Tips To Organize Python Code

Tech With Tim

2 года назад

207,572 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

ryan derksen
ryan derksen - 19.09.2023 00:44

I need to learn to structure code properly .

Ответить
Flexonze
Flexonze - 07.09.2023 00:35

For the fifth tip about sorting imports, I would suggest looking into isort. It's a library that sorts your imports automatically. You can easily set it up to fit your personnal preferences.

Ответить
sina orojlo
sina orojlo - 27.08.2023 00:01

Its super niceee 🎉

Ответить
Dr Gamma D
Dr Gamma D - 08.08.2023 01:41

Here's how it goes, for a module:

"""docstring"""

import [python standard library]
import [public 3rd party libs]
import [private 3rd party (your other packages)
from package import module (local package imports)

from . import stuff (relative imports)

CONSTANTS = "module constants"

def func():
"""module functions: pure function with no state and no side effects"""

class Noun
"""classes in the module. Classes are Nouns, they respect things"""

class_attrs = "class attribute 1st""

@classmethod
def from_other_information(cls, ....)
"""constructor helpers, iff WORK needs to be done to convert args to instance attributes""
return cls(..., attr)

def __init__(self, ..., attr):
"""though shalt not do work in init!"""
super().__init__(...) # call super 1st, if needed
self.attr = attr # just set attrs in init, Do. Not. Do. Work.

@property
def derived_attr(self):...
"""now include properties, if needed"""

def __magic_methods__(self...)
"""1st define unary, then binary, ternary magic methods"""

def _protected_method(self....):
"""...from lowest level on up. Alway push work up the method chain as far as possible"""

def public_method(self...):
"""here and only here will you change the state of self, or further up in a protected method""""

def the_single_responsibilty_method(self..):
"""since your class has a single responsibility, only one reason to change, the raison detre will be
in the last method that calls all other methods"""


unit_test() # if you do that.

Ответить
Leon Derczynski
Leon Derczynski - 11.05.2023 00:10

After tip 2 I was expecting tip 3 to be "switch to coding in Eclipse"

Ответить
Philco
Philco - 20.04.2023 10:14

Excellent tips.. thanks!

Ответить
YouTube Summariser
YouTube Summariser - 11.04.2023 12:39

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.

Ответить
Omlette
Omlette - 03.03.2023 09:30

Just starting my journey here and I am enjoying this content from over the years. Great stuff Tim

Ответить
reverseila *
reverseila * - 27.02.2023 18:56

Thanks!

Ответить
Comm_GT
Comm_GT - 06.01.2023 06:39

sophie the cat is adorable

Ответить
ExpansiveGymnast10
ExpansiveGymnast10 - 04.01.2023 15:12

THANK YOUUUUUUU! I am doing a Python project for college and this is exactly what I needed.

Ответить
Rishi Chougule
Rishi Chougule - 04.01.2023 10:37

I give you like for Sophie🥰😄...

Ответить
Mahshid Heshmati
Mahshid Heshmati - 21.12.2022 20:59

Can you take a course for computer olympiad topics?🙏

Ответить
Adam Strejcovský
Adam Strejcovský - 23.10.2022 12:00

cat :)

Ответить
fabulous
fabulous - 15.10.2022 14:12

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 ❤

Ответить
devsutong
devsutong - 18.09.2022 12:40

how to import packages that is not on the sane directory? I've tested relative path but it's not working

Ответить
Zyhon
Zyhon - 07.08.2022 04:57

Thank you very much. Very useful tips. Just subscribed to ProgrammingExpert.

Ответить
NoWifi
NoWifi - 10.07.2022 00:38

Do I have to import a module (such as Pygame) into each module/package _init_ file that I create myself?

Ответить
mohmed badr
mohmed badr - 20.06.2022 17:34

whats peppe et?

Ответить
Jason Tai
Jason Tai - 02.05.2022 15:53

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?

Ответить
Dennis Vl
Dennis Vl - 30.04.2022 15:51

Absolutely disagree with one class one file. This ain't Java.

It's in the name. "Module". It is supposed to contain multiple classes relating to a functionality. When a module starts growing a bit large, that's when you should consider refactoring something that stands out into its own module. Or subpackages for major parts of a system. Reading a file that is just a bit too large is infinitely preferable to trudging through dozens of files that link together to form a single thing in your program. Consider the mental cost of having to keep lots of separate files and keeping track of their structure.

Ответить
KosmonautOfficial
KosmonautOfficial - 12.04.2022 19:47

Great video thanks so much! I have been using modules but not local packages thanks!

Ответить
Melissa Leigh
Melissa Leigh - 05.04.2022 17:59

Thanks

Ответить
Wonder Boy
Wonder Boy - 02.04.2022 07:10

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!

Ответить
gilbert Jizz
gilbert Jizz - 20.03.2022 02:03

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

Ответить
Balázs Horváth
Balázs Horváth - 15.03.2022 01:40

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

Ответить
Євген Панаско
Євген Панаско - 14.03.2022 15:02

Great video, very useful. Thanks.

Ответить
Joni Hanski
Joni Hanski - 14.03.2022 01:58

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.

Ответить
Hendrik D
Hendrik D - 14.03.2022 01:41

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...

Ответить
ewetoobie
ewetoobie - 12.03.2022 07:58

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?

Ответить
Arjix
Arjix - 10.03.2022 16:51

My import order is based on the length of the line, and in descending order

Ответить
Christoph Backhaus
Christoph Backhaus - 10.03.2022 16:49

Ever variable has its own Datatype. If a variable changes it is not the same Datatype anymore.

Ответить
Christoph Backhaus
Christoph Backhaus - 10.03.2022 16:47

I have a rule. One Function = One File

Ответить
Brian Ferrell
Brian Ferrell - 10.03.2022 02:38

needed this

Ответить
jomy10
jomy10 - 10.03.2022 00:31

What VS Code theme do you use?

Ответить
ComputerrCat
ComputerrCat - 09.03.2022 08:04

i am more focused on the cat

Ответить
Elias Ebner
Elias Ebner - 09.03.2022 03:11

OMG, THIS WAS ACTUALLY SO USEFUL. Thanks a lot, really learned a lot.

Ответить
Elisa Chin
Elisa Chin - 08.03.2022 07:30

Nice offer and tips from this video, it has given me a sense of relief.

Ответить
George Hammond
George Hammond - 07.03.2022 19:21

great tips. but would be great to do more on point #1 - Use Modules and Packages.

Ответить
Beat Mars go69
Beat Mars go69 - 07.03.2022 17:33

I saw the cat 🐱😳

Ответить
i hate coding in c based languages
i hate coding in c based languages - 07.03.2022 16:59

wow look at the cat on the intro :D

Ответить
Mr. Tiến
Mr. Tiến - 07.03.2022 15:45

Love this

Ответить
Umesh Lab
Umesh Lab - 07.03.2022 08:47

Sophie is cute
@Tech with Tim how many cats do you have?

Ответить
Educate Yourself Daily
Educate Yourself Daily - 07.03.2022 07:43

Hey Tim, will you 💞 know me which software you are using for video 📷 editing Please...

Ответить
Educate Yourself Daily
Educate Yourself Daily - 07.03.2022 07:37

Hey Tim 👍

Ответить
Brian Richard
Brian Richard - 06.03.2022 21:41

Hi Tim! great video , i enjoyed it and now i feel like i can easily take on python

Ответить
Cristino Canga
Cristino Canga - 06.03.2022 14:38

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

Ответить