Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Welcome to the course
Lecture: Welcome and thanks for coming
0:01 Hello and welcome to the course Write Pythonic Code Like a Seasoned Developer.
0:04 My name is Michael Kennedy and we are going to be on this journey together
0:07 to help you write more readable, more efficient and more natural Python code.
0:13 So what is Pythonic code anyway?
0:16 When developers are new to Python, they often hear this phrase Pythonic
0:20 and they ask what exactly does that mean?
0:23 In any language, there is a way of doing things naturally
0:26 and a way that kind of fight the conventions of the language.
0:30 When you work naturally with the language features and the runtime features,
0:33 this is called idiomatic code,
0:35 and in Python when you write idiomatic Python we call this Pythonic code.
0:40 One of the challenges in Python is it's super easy to get started
0:43 and kind of learn the basics and start writing programs
0:46 before you really master the language.
0:48 And what that often means is people come in from other languages
0:51 like C++ or Java or something like that,
0:53 they will take algorithms or code that they have and bring it over to Python
0:57 and they will just tweak the syntax until it executes in Python,
1:00 but this often uses the language features of - let's just focus on Java.
1:04 In Python there is often a more concise, more natural way of doing things,
1:09 and when you look at code that came over from Java,
1:11 we'll just take a really simple example-
1:13 if you have a class and the class has a get value and set value,
1:17 because in Java that is typically the way you do encapsulation,
1:20 and people bring those classes in this code over and migrate it to Python,
1:24 they might still have this weird getter/setter type of code.
1:28 And that would look completely bizzare in Python
1:31 because we have properties for example.
1:33 So we are going to look at this idea of Pythonic code,
1:36 now, it's pretty easy to understand but it turns out to be fairly hard to make concrete,
1:40 you'll see a lot of blog posts and things of people trying to put structure
1:44 or examples behind this concept of Pythonic code.
1:48 We are going to go through over 50 examples of things I consider Pythonic
1:53 and by the end you'll have many examples,
1:56 patterns and so on to help you have a solid grip on what Pythonic code is.
2:02 So what is Pythonic code and why does it matter?
2:05 Well, when you write Pythonic code,
2:07 you are leveraging the experience of 25 years of many thousands,
2:10 maybe millions of developers,
2:12 these guys and girls have worked in this language day in and day out
2:16 for the last 25 years and they really perfected the way
2:19 of working with classes, functions, loops, and so on,
2:23 and when you are new especially,
2:25 it's very helpful to just study what those folks have done and mimic that.
2:29 When you write Pythonic code, you are writing code
2:32 that is specifically tuned to the CPython runtime.
2:35 The CPython interpreter and the Python language have evolved together,
2:40 they have grown up together,
2:42 so the idioms of the Python language are of course matched
2:44 or paired well with the underlying runtime,
2:47 so writing Pythonic code is an easy way
2:50 to write code that the interpreter expects to run.
2:54 When you write Pythonic code,
2:56 you are writing code that is easily read and understood by Python developers.
2:59 A Python developer can look at standard idiomatic Python
3:03 and just glance at sections and go,
3:05 "oh, I see what they are doing here, I see what they are doing there, bam, bam, bam"
3:09 and quickly understand it.
3:10 If instead it's some algorithm that is taken from another language with other idioms,
3:15 the experienced developer has to read through and try to understand
3:20 what is happening at a much lower level,
3:23 and so your code is more readable to experienced developers
3:27 and even if you are new will become probably more readable to you
3:30 if you write Pythonic code.
3:32 One of the super powers of Python is that it is a very readable
3:36 and simple language without giving up the expressiveness of the language.
3:40 People coming from other languages that are less simple,
3:43 less clean and easy to work with
3:46 will bring those programming practices or those idioms over
3:49 and they will write code that is not as simple as it could be in Python
3:53 even though maybe it was a simple as it could be in C.
3:56 So when you write Pythonic code,
3:57 you are often writing code that is simpler and cleaner
4:00 than otherwise would be the case.
4:02 If you are working on an open source project,
4:04 it will be easier for other contributors to join in because like I said,
4:08 it's easier for them to read and understand the code at a glance,
4:11 and they will more naturally know what you would expect them to write.
4:14 If you are working on a software team,
4:16 it's easier to onboard new Python developers into your team
4:20 if you are writing idiomatic code,
4:22 because if they already know Python
4:23 it's much easier for them to grok
4:26 your large project.