From the course: Python for JavaScript Developers

Why style is crucial - Python Tutorial

From the course: Python for JavaScript Developers

Start my 1-month free trial

Why style is crucial

- [Teacher] In the television show, "Silicon Valley," there's a couple that breaks up over a very major issue. He prefers to use tabs for indentation while she indents using spaces. Yes, style is kind of a big deal and subject for many heated debates amongst programmers. And I think it's important that we talk about style when it comes to Python and JavaScript. And I think that's kind of important to get started with that early to kind of adapt the right habits. So in JavaScript you see style varying from company to company. I've been in companies that don't use semi-colons whatsoever. I've worked in companies who swear by semi-colons. Naming conventions can vary from company to company. And while there are some industry accepted conventions like using three equal signs in comparisons for instance, overall you'll see many variants. Python coding style does vary from company to company but much less significantly. Overall, there's a style guide called PEP 8, which has been accepted as the official style guide of the language. And it's not just important in terms of how the code looks. People will read code and make certain assumptions based on conventions. And you'll see how in a minute. So on an important note while the interpreter will let you write code in various ways, it's a good idea to get started on the right foot and not adapt habits that will make your code less legible. So with that said, let's take a look at some naming conventions. So in Python constants are defined with all caps, separated by underscores. Variables use what's called snake-case. This is lower case using underscores to separate words and classes use what's called Kabab-case. This is similar to camel-case but the first letter is capitalized as well. So basically all the words are glue together and each word is capitalized on the first letter. There are also special methods that many people will refer to as dunder methods. And these methods are named using an underscore, underscore in the beginning and an underscore, underscore at the end. And when it comes to dunder methods, they have to use this convention for them to function properly. So this would be dunder init for instance. So let's head over to some code and compare a little bit of JavaScript code with a little bit of Python code. So here I am in my exercise files at 02_01.js and to the right is 02_01.py. And you'll see I've written a few code examples of these conventions. And in the beginning, you will see const myName, which you will see sometimes defined in the same way in JavaScript, using all caps, separated by an underscore but many times you'll also see camel-case, it varies. And you do have const which is an indicator of a constant whereas in Python you're using this style in order to indicate to other developers not to override this variable. And then we have the function sayHi, which on the left is classic camel-case on the JavaScript side and on the right you'll see that it uses snake-case. Then there is the class of Pet that has a constructor. You see the capitalized beginning and on the right you'll see the dunder init method, which is similar to the constructor in JavaScript. And it has to be dunder init for the interpreter to understand that. We then instantiate a Pet on the right using all lower case in Python and then we use the print method. So let's just run this code to see what happens. So here I am at my terminal and I just want to make sure that my virtual environment is activated. So I'll do a source venv/bin/activate, Yup, and clear my terminal. And I'm going to go ahead and run the JavaScript first. So say node CH 02 01.js. And I'll do the same thing with Python. CH 02 01 .py this time. And same output, different programming language, different style. So hopefully this gave you some things to pay attention to throughout the course. And really pay attention in the course examples to conventions that I use in code. And this will help you get started on the right foot and we'll make it easier for other Python programmers to read your code and use your APIs.

Contents