Комментарии:
I need to learn to structure code properly .
Ответить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.
ОтветитьIts super niceee 🎉
Ответить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.
After tip 2 I was expecting tip 3 to be "switch to coding in Eclipse"
ОтветитьExcellent tips.. 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.
Just starting my journey here and I am enjoying this content from over the years. Great stuff Tim
ОтветитьThanks!
Ответитьsophie the cat is adorable
ОтветитьTHANK YOUUUUUUU! I am doing a Python project for college and this is exactly what I needed.
ОтветитьI give you like for Sophie🥰😄...
ОтветитьCan you take a course for computer olympiad topics?🙏
Ответитьcat :)
Ответить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 ❤
how to import packages that is not on the sane directory? I've tested relative path but it's not working
ОтветитьThank you very much. Very useful tips. Just subscribed to ProgrammingExpert.
ОтветитьDo I have to import a module (such as Pygame) into each module/package _init_ file that I create myself?
Ответитьwhats peppe et?
Ответить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?
Ответить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.
Great video thanks so much! I have been using modules but not local packages thanks!
ОтветитьThanks
Ответить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!
Ответить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 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, very useful. 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.
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...
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?
ОтветитьMy import order is based on the length of the line, and in descending order
ОтветитьEver variable has its own Datatype. If a variable changes it is not the same Datatype anymore.
ОтветитьI have a rule. One Function = One File
Ответитьneeded this
ОтветитьWhat VS Code theme do you use?
Ответитьi am more focused on the cat
ОтветитьOMG, THIS WAS ACTUALLY SO USEFUL. Thanks a lot, really learned a lot.
ОтветитьNice offer and tips from this video, it has given me a sense of relief.
Ответитьgreat tips. but would be great to do more on point #1 - Use Modules and Packages.
ОтветитьI saw the cat 🐱😳
Ответитьwow look at the cat on the intro :D
ОтветитьLove this
ОтветитьSophie is cute
@Tech with Tim how many cats do you have?
Hey Tim, will you 💞 know me which software you are using for video 📷 editing Please...
ОтветитьHey Tim 👍
ОтветитьHi Tim! great video , i enjoyed it and now i feel like i can easily take on python
Ответить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