Write Python code people actually want to use

  Рет қаралды 11,661

Doug Mercer

Doug Mercer

Күн бұрын

Sign up for 1-on-1 coaching at dougmercer.dev
-----------------------------------------
Writing code that "works" shouldn't be your end goal. It's really just the start...
In this video, we adapt a clumsy, non-Pythonic API into an easy to use, easy to understand Pythonic one. We use magic methods such as _getitem__, __len__, __enter__, and __exit_ to make our objects a context manager and support the len() function and square bracket indexing. And in the end, we turn what once was ugly, difficult to maintain code into something that other developers would actually want to use.
This video is inspired by the wonderful PyCon talk by Raymond Hettinger, "Beyond PEP8". I just wanted a distilled down, 8 minute version that I could share with people.
#python #objectorientedprogramming
Chapters
---------------
0:00 Intro
0:38 The Problem
2:23 The Client
4:53 Everything Else
6:27 What did we do?

Пікірлер: 72
@rhine3
@rhine3 9 ай бұрын
I have no knowledge of anything related to coding, yet I still watched to the end. Your editing style just hooks me in!
@dougmercer
@dougmercer 9 ай бұрын
That's *such* a nice compliment-- you made my day! thanks so much for commenting =]
@wtfomgok
@wtfomgok 3 ай бұрын
Wait a minute... Did the algorithm just suggest something meaningful to me out of nowhere? Exactly the problem I'm working on perfectly solved in an interesting manner! Thanks! Subscribed.
@dougmercer
@dougmercer 3 ай бұрын
Whew, I hope the algorithm keeps up, so I don't need to pivot to a True Crime Podcast + Slime + Subway Surfer content 😥 Thanks for watching and commenting =]
@poketopa1234
@poketopa1234 9 ай бұрын
Honestly such a good video. Compared to the typical "Here's how to level up your python skills by using tuples", this video speaks to the viewer at exactly the right level and actually contributes something valuable and interesting.
@dougmercer
@dougmercer 9 ай бұрын
That's such a nice compliment =]. thanks for watching!
@nullzeon
@nullzeon Ай бұрын
joke at 5:32 is gold! such a great channel
@uzayrsyed6293
@uzayrsyed6293 Ай бұрын
The Stream class to me seems like a layer of indirection it's not doing much except making methods as properties and hiding useful documentation that may be associated with these methods. Props for the amazing video quality!
@MaxShapira2real
@MaxShapira2real 9 ай бұрын
Top-notch content. Thank you! A comprehensive Python course (by you) would be a real benefit to the community. A course to guide you through intermediate topics like writing better functions & classes, type hints, decorators, generators, magic methods, useful methods of different data types, asynchronous programming, and data structures (Lists, Tuples, Dictionaries, Sets, Stacks, Queues, Linked Lists, Hash Tables, Trees, and Graphs).
@dougmercer
@dougmercer 9 ай бұрын
Thanks so much! I'll definitely consider making a course at some point. That said, if it takes me a month to put together like 10 or 20 minutes of video, I shudder to think how long a whole course would take me to put together 😨 Maybe if I ever turn this into a full time career 🤔
@capability-snob
@capability-snob Ай бұрын
I had completely forgotten that all sequences are iterable - I'm so used to implementing __iter__/__next__!
@Michael-eo8ro
@Michael-eo8ro 4 ай бұрын
Wow, this vid blew my mind 8 times over. So many things to try!
@dougmercer
@dougmercer 4 ай бұрын
Thanks! Have fun =]
@kenneth.topp.
@kenneth.topp. 9 ай бұрын
This kinda reminds me of Hettinger's Beyond PEP 8 talk. I would add pydocs and typing to the list of things to do to make your package easier on developers. Also, when I create a context manager, and even if I recommend it, I don't prevent the user from doing it by hand by leaving the cleanup code in the context manager exit method. Sometimes the user "knows what they are doing" and for some reason doesn't want a context manager. making sure you expose the raw capabilities is nice, if you haven't properly anticipated all the use cases of your package.
@dougmercer
@dougmercer 9 ай бұрын
Yeah, this video is basically Beyond PEP8, boiled down to 8 minutes. I asked about a dozen coworkers if they had watched it and they all said "no, too long." I thought that was a shame and tried to modernize, simplify, and condense it. Definitely agree with docs and type hints! I'm a huge fan of both (my first video was actually on type hints, but it's basically just a slideshow). I'll have to make a video on docs sometime soon 🤔. Thanks for the idea! And true, it can be good to expose the underlying raw functionality. When I'm teaching people, I tend to advise against it if only to avoid situations where developers create "foot guns" for their users. I think it's good to design the primary work flow in a way that there are no foot guns. Thanks for watching the video and the thoughtful comment =]
@kenneth.topp.
@kenneth.topp. 9 ай бұрын
@@dougmercer hah. good work then. not sure I'm a fan of calling 'dunders' magic methods, but we'll see if they catch on :)
@thisoldproperty
@thisoldproperty 2 ай бұрын
What a great video! Just be aware that implementing the context manager in Python is not fail-safe. If you execute the code on a loop it will actually fail at some point. I agree with the approach, but at times have to take it a step further by implementing the solution in C.
@dougmercer
@dougmercer 2 ай бұрын
Good point!
@ThisRussellBrand
@ThisRussellBrand Ай бұрын
This is a beautiful explanation. Thank you for sharing it.
@dougmercer
@dougmercer Ай бұрын
Glad it was helpful!
@jamesborden7105
@jamesborden7105 Ай бұрын
You make such great Python content! Please keep it up!
@dougmercer
@dougmercer Ай бұрын
Thanks, will do =]
@jamesarthurkimbell
@jamesarthurkimbell 2 ай бұрын
Best channel I've found in a while. Keep em coming, king
@dougmercer
@dougmercer 2 ай бұрын
Thanks James! Will do-- I've got my next few videos planned out, and should hopefully have the next one done in April. hope to see you then =]
@ben-brady
@ben-brady 2 ай бұрын
This was an incredibly good example of good API design, amazing video. However I prefer not to use properties for anything non-trivial as it tends to hide that work is being done
@dougmercer
@dougmercer 2 ай бұрын
Thanks! That's totally fair. Properties can be a bit surprising/implicit. But, despite that, I do still like them =]
@NoName-tt9ye
@NoName-tt9ye 9 ай бұрын
Great video! I love the use of music in it.
@dougmercer
@dougmercer 9 ай бұрын
Thanks so much!
@Silentstrike46_
@Silentstrike46_ 9 ай бұрын
Very interesting video! Been working with Python heavily over the past few years, but didn't know these existed - already seeing how using context managers would have helped deal with some of the more problematic modules I've either had to use, or wrote myself. Definitely new skills in the toolbox! You've got a new subscriber, quite excited to see what new videos you make!
@dougmercer
@dougmercer 9 ай бұрын
Glad to hear you found it helpful! thanks for taking the time to watch, subscribe, *and* comment =]
@zariy2164
@zariy2164 9 ай бұрын
Oh wow I didn’t except to see a great video from such a small KZbinr. I really like it and I learned some Python features that I didn’t knew about, you deserve more visibility 💯 You definitely won a sub 👍
@dougmercer
@dougmercer 9 ай бұрын
Thanks, zariy! Just getting started =]
@SilkroadOnlineGlobal
@SilkroadOnlineGlobal 9 ай бұрын
Nice work, bro! Keep coming. Thx👍👍
@dougmercer
@dougmercer 9 ай бұрын
Thanks, will do! =]
@mahdirasouli6005
@mahdirasouli6005 6 ай бұрын
Hey man. I hope you continue making your high quality contents. Perhaps only channel in KZbin that I watched everything in that
@dougmercer
@dougmercer 6 ай бұрын
Thanks so much =] New video definitely coming mid-to-late December, and then I have a few other videos in various stages of development.
@mahdirasouli6005
@mahdirasouli6005 6 ай бұрын
@@dougmercerOne thing regarding your contents is the amount of time you spend edit your videos. My understanding as a consumer of your videos is you can reduce these vide/audio effects at this stage and make more contents, then after 20k sub you can again bring the effects
@dougmercer
@dougmercer 6 ай бұрын
Definitely agree. Since my last upload, I've been doing a few things to make the process of creating videos easier and find some sponsors that I believe in that make it easier to recoup costs. In 2024 I'll produce content more consistently =]
@Saddl3r
@Saddl3r Күн бұрын
Great editing. What programs do you use? A "making-of" video would be very interesting!
@dougmercer
@dougmercer Күн бұрын
Thanks! This video I used a mixture of Manim to render the syntax highlighted code and Davinci Resolve to do the call outs. In later videos I use exclusively Davinci Resolve (+ a Python script to turn my code into a syntax highlighted fusion composition). I'm actually working on writing my own animation library that's similar to manim, but is more tailored for me/the sort of videos I make. I plan on making a video about it as one of my next two videos. so, stick around for that =]
@Saddl3r
@Saddl3r Күн бұрын
@@dougmercer Thanks!! 🌟
@cemrehancavdar
@cemrehancavdar 9 ай бұрын
Great video! I knew this stuff but given example was top notch, i will recommend this video whenever it needs to be.
@dougmercer
@dougmercer 9 ай бұрын
Thanks =]
@thaejsooriya3313
@thaejsooriya3313 9 ай бұрын
This was great, going to try and use this where I can. You have earned yourself a new sub good sir, hopefully this comment appeases the algorithm gods
@dougmercer
@dougmercer 9 ай бұрын
Thanks so much! Engagement for the Algo-Gods! 🙌
@saeedxgholami
@saeedxgholami 9 ай бұрын
Great video, short nice very well put together. Thank you.
@dougmercer
@dougmercer 9 ай бұрын
Thanks!
@user-jt4bv9xt1p
@user-jt4bv9xt1p 9 ай бұрын
Doug, this was a great video full of great advice.
@dougmercer
@dougmercer 9 ай бұрын
Thanks! Glad it was helpful =]
@silkogelman
@silkogelman 9 ай бұрын
Very interesting and well-produced video! Thank you so much for this Doug. 🙏😀 Subscribed and hoping for more of these kinds of Python videos. Might be interesting to add a GitHub link to the description with before / after situation to be able to see the entire thing. (it would help my comprehension while watching you focus on and explain the specific parts in the video)
@silkogelman
@silkogelman 9 ай бұрын
P.S. The 5 tips 'summary' at the end is awesome! 👍
@dougmercer
@dougmercer 9 ай бұрын
Thanks for watching! I have a few more videos like this on my channel, so check them out until I produce the next one! I try to make a video every couple months (I'm slow 😭) Currently I have a mix of the code I discuss + the manim animations used in the video (if any) stored in private Gitlab repos. I'll think about making them available to viewers in the future 🤔
@silkogelman
@silkogelman 9 ай бұрын
​@@dougmercer Already watching your video about the Magic that Makes Python Tick. 😀And I'm rewatching your videos because they are packed with golden nuggets of knowledge. 🙏 Maybe 1 or 2 monthly short videos (YT Shorts) would be interesting for your channel. One concept explained well in the beautiful visual, warm, relaxed and well-paced style you create.
@Pankaj-Verma-
@Pankaj-Verma- 3 ай бұрын
Clean.
@dougmercer
@dougmercer 3 ай бұрын
Thanks!
@JuSiPa
@JuSiPa 9 ай бұрын
Thanks!
@dougmercer
@dougmercer 9 ай бұрын
Wow! Thank YOU! You're my first ever super thanks for *any* video. Thanks a lot for supporting the channel!
@JuSiPa
@JuSiPa 9 ай бұрын
@@dougmercer no worries - the first of many I hope.
@user-uf3ps6pu3r
@user-uf3ps6pu3r 9 ай бұрын
Looks good. I'd like to diff the 2 code sets just to get a better sense of the improvements. Do you have the code in github or something else?
@dougmercer
@dougmercer 9 ай бұрын
Yes-- I make the code presented in my videos available in a GitHub repository accessible by my monthly GitHub Sponsors, github.com/sponsors/dougmercer
@darcash1738
@darcash1738 2 ай бұрын
I never understood why we needed getter methods. What is it doing that object.attribute isn’t already doing? Like we set up all the self.attribute = whatever in the class so why not use that?
@dougmercer
@dougmercer 2 ай бұрын
It's a hold over from Java. They couldn't easily add behavior (like validation, lazy loading, etc.) on the property. So, in case they ever needed to add behavior in the future, they proactively used getter and setter methods. This made it so that they wouldn't have to change every reference to the attribute later if they wanted to modify its behavior. In Python, get_ and set_ methods are totally pointless, because we have other ways of implementing behavior for both.
@darcash1738
@darcash1738 2 ай бұрын
@@dougmercer oh ok 😂 but let’s say I want to raise some sort of exception if one of the attributes isn’t in the range I want. Is it more standard to do a getter? Would it look out of place if I did it in the init
@dougmercer
@dougmercer 2 ай бұрын
If the value is set at initialization, it totally makes sense to do validation at initialization. If the value is only available when you are "setting" it after initialization, then you can use the setter decorator, like this docs.python.org/3/library/functions.html#property.setter
@Han-ve8uh
@Han-ve8uh 9 ай бұрын
What's StreamManager (4:49) and Stream (5:50)?
@dougmercer
@dougmercer 9 ай бұрын
Good question! The "ye olde API" had three objects: Client, StreamManager, and Stream. In our example, the client would return a StreamManager whenever we called client.getDataStreams(). Further, the StreamManager would return a Stream from StreamManager.getStreamByIndex. Since, in our example, this "ye olde API" is a third party library, we can't really go out and refactor it directly. However, we can "adapt" it by creating new versions of those classes that use the old objects (through composition) internally and make them easier to use. Hope that helps!
@Divyv520
@Divyv520 9 ай бұрын
Hey Doug , really nice video! I was wondering if I could help you edit your videos and also make a highly engaging Thumbnail which will help your video to reach to a wider audience .
@dougmercer
@dougmercer 9 ай бұрын
Thanks Sahil. Do you have any examples of your work? This channel is not really profitable, so I can't really afford to hire anyone right now.
@Divyv520
@Divyv520 9 ай бұрын
Yeah ! I will send it to you
@dearheart2
@dearheart2 Ай бұрын
Gave up to watch video, too difficult to hear the voice over the "music"
The Magic that Makes Python Tick
15:21
Doug Mercer
Рет қаралды 2,4 М.
Compiled Python is FAST
12:57
Doug Mercer
Рет қаралды 91 М.
[柴犬ASMR]曼玉Manyu&小白Bai 毛发护理Spa asmr
01:00
是曼玉不是鳗鱼
Рет қаралды 51 МЛН
He tried to save his parking spot, instant karma
00:28
Zach King
Рет қаралды 18 МЛН
Indian sharing by Secret Vlog #shorts
00:13
Secret Vlog
Рет қаралды 59 МЛН
STOP Using Plain Python Scripts! Do this instead (5 reasons)
15:47
How Fast can Python Parse 1 Billion Rows of Data?
16:31
Doug Mercer
Рет қаралды 156 М.
Unlocking your CPU cores in Python (multiprocessing)
12:16
mCoding
Рет қаралды 291 М.
Python's 5 Worst Features
19:44
Indently
Рет қаралды 82 М.
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Рет қаралды 91 М.
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 887 М.
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 84 М.
The Absolute Best Intro to Monads For Software Engineers
15:12
Studying With Alex
Рет қаралды 572 М.
Cohesion and Coupling: Write BETTER PYTHON CODE Part 1
26:14
ArjanCodes
Рет қаралды 212 М.
5 Good Python Habits
17:35
Indently
Рет қаралды 348 М.
Задача APPLE сделать iPHONE НЕРЕМОНТОПРИГОДНЫМ
0:57
Карточка Зарядка 📱 ( @ArshSoni )
0:23
EpicShortsRussia
Рет қаралды 518 М.
Power up all cell phones.
0:17
JL FUNNY SHORTS
Рет қаралды 50 МЛН
Pratik Cat6 kablo soyma
0:15
Elektrik-Elektronik
Рет қаралды 8 МЛН
С Какой Высоты Разобьётся NOKIA3310 ?!😳
0:43
iPhone 15 Pro vs Samsung s24🤣 #shorts
0:10
Tech Tonics
Рет қаралды 10 МЛН