Naming ConventionsDuring a recent workshop, bootcamp instructor Alex Baransky shared some of the best practices in writing Python code for better readability. Through this series of articles, we will summarize and give you a few examples of those best practices to help you write more elegant Python code, benefiting those who may read and use your code in the future, including yourself. Show Python is an object-oriented programming language. This means you’ll create, name, and use all kinds of objects in any given Python project. Naming conventions are the rules of thumb for naming the objects that you create. Understanding these conventions is the first step to keeping your code readable for you and your teammates. Through our bootcamps and courses, we share to students that there are tons of ways to name objects, some being better than others. Understanding the difference between good and great is the way to impress prospective employers and peers. In this article, we will share best practices in naming conventions that you can follow and adopt when writing your Python code. Basic Naming ConventionsLet’s start with the most common options for naming objects in Python. The following are basic ways to name:
Each of these options has different use cases. They are not only for aesthetics, but each option also helps you create meaningful objects in your code. Names to AvoidJust as there are things you should do, there are things you shouldn’t. These are conventions you will want to avoid when writing Python code.
We avoid those names because in some fonts, they are indistinguishable from the numbers “1” and “0”, which makes it difficult for the user to understand what’s going on. You should also avoid using Python keywords and built-in class/function names for your variable names. For example, words like “max,” “sum,” “class,” and “list” are words that already exist in any Python environment. Therefore, to use them for something other than their function could make things messy and confusing. Modules and PackagesA module is a collection of pre-built functions and other objects used to perform certain tasks. Modules should have short, lowercase names. Underscores can be used in the module name if it improves readability (like for names with multiple words). Packages are like directories that contain modules and other objects. Python packages should follow the same convention, although the use of underscores in package names is discouraged. Variables and FunctionsDo you know the difference between a function and a method?To understand the difference, think of the square-rectangle relationship: a method is a function, but not all functions are methods. A method is a special type of function that belongs to a class. Only objects of that class type can use these special functions. This is part of the object-oriented paradigm of Python and can help improve the organization and readability of our code. In Python, the names of variables and functions should be lowercase. Individual words can be separated by underscores when needed. This will improve readability within your code. Method names should follow the same conventions as function names. Here are a few best practices to follow when naming your variables and functions:
Understanding and adopting these best practices is a great way to polish your coding skills. Writing more elegant code will not only impress your current colleagues, but it will also help you build better coding habits that may catch the attention of future employers. Ready to advance your programming skills? Check out this three-course program focused on building and advancing your Python Programming skills, or start your journey towards data science mastery by enrolling in our upcoming remote live and online bootcamps this Winter. What are variable naming conventions?A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign " $ ", or the underscore character " _ ". The convention, however, is to always begin your variable names with a letter, not " $ " or " _ ".
What are the 3 rules for naming a variable?Go variable naming rules: A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ )
Is Python a Camelcase or Snakecase?Python, by contrast, recommends snake case, whereby words are instead separated by underscores ( _ ), with all letters in lowercase.
What is a variable convention?Variables are tokens within the command statements that are to be replaced with actual values immediately before the statement is run.
|