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