At its core, Python is an object-oriented programming (OOP) language. Everything in Python — modules, classes, and instances — are objects. One essential building feature is the owning of various attributes for each object. If you come from other OOP languages, you may be used to setters and getters. However, that’s not something that you hear about a lot in Python coding. Yet, it’s a fundamental topic that you should know about. In this article, I’d like to review six key things points about accessing attributes.
Encapsulation is a programming style that separates externally-exposed functionalities from internal implementation details. …
No matter what projects you’re doing with Python, the use of variables is inevitable. As a result, the prevalence of variables makes them a common topic of interviews for any Python-programming-related positions. In this article, I would like to discuss three tricky questions about variables.
Before we start, let’s briefly review what variables are. In the code below, we have four variables in the following built-in data types:
list. They are all pretty straightforward, right? Sure they are. However, many advanced questions concerning variables can arise from these simple variables. …
If your program is a factory, functions are its workers that maneuver the machinery to make your program run. In a typical factory, there are different types of workers tasked with varied jobs. Correspondingly, your program should be versatile in terms of its functions. To write robust functions, your coding arsenal should include the necessary tools to allow you to equip your program with the best workers.
In this article, I’d like to explain five advanced concepts related to functions in Python. …
In many of our applications, we need to deal with synchronous programming only. This means that code will execute linearly on a single thread. The following code shows you a trivial example:
load_data_for_userfunction calls the
Apparently, the user experience isn’t too great if the user has to wait five seconds before the data can be displayed. That’s where asynchronous programming can be particularly helpful. Take a look at the following updated code:
Although each of the data types has its own pros and cons, dictionaries are my favorite among the common data containers (e.g. lists and sets). To become an experienced Python developer, you need to be knowledgeable about this versatile data type.
Without further ado, I’d like to share some tips beyond the most basic use cases of dictionaries in this article. I hope you’ll find them helpful.
When you write a Python script, you may need to define some global variables. However, when you update these variables in your functions somewhere later, you have to be explicit about these variables being global. …
Writing code is difficult. Your code can’t run if your syntax is wrong, if your parameters are wrong, or if your data are corrupted. There are hundreds or thousands of things that make your code unable to execute.
Writing readable code is even more difficult. After learning the language and pertinent frameworks, you’re finally able to write some code that your computer understands. However, chances are that others can’t read your code — even with the help of your helpful (at least in your mind) comments.
The problem of code’s readability doesn’t happen only to junior developers, but also to seasoned developers if they haven’t learnt the right habit to begin with. In this article, I’d like to share the tips that I use to write code that is readable and thus maintainable in the long term by following just one principle — consistency. …
We always use functions in our projects — no matter what our projects are about. When we call functions that are written by others, we need to understand the function signature (i.e. what parameters we should pass to get the desired function output). Sometimes, we need to write functions for others to call when we should be clear about the desired use cases without causing any ambiguity. In either case, it’s not the easiest job.
In this article, I’d like to provide a comprehensive overview of arguments in Python functions. Specifically, I’m referring to the input parameters. …
Learning a new programming language is not easy, especially if you don’t have any previous programming experience. However, learning Python is arguably more straightforward than learning several other languages because it endorses the REPL (Read-Eval-Print-Loop) learning approach for beginners. Simply put, to learn Python, we can just use a command console. You write your Python code (e.g.
print(“Hello, World!”)), and the console will evaluate the code and print the output as applicable. This REPL approach provides real-time feedback about varied topics in Python, which makes Python learning straightforward.
Despite this handy learning approach, there are some concepts that may be confusing to Python beginners. …
The most frustrating thing in software development is to debug a problem that you’re unable to reproduce. You may have to rely on various anecdotal descriptions from some less tech-savvy end-users to guess what happened — if you didn’t set up the logging properly before shipping your software. However, if you happen to have adequate logging information that can help you trace the source of the problem, it will probably take much less time to remove the bug.
In this article, I’d like to go through five questions that you may have regarding logging in your Python projects. The main focus of this article will be the built-in logging module. …
Data are the most essential element in any programming project. All programs are destined to have certain interactions with data. For instance, if your project is a website, you have to present data (e.g., texts and images) in a user-friendly manner. If your project uses machine learning to predict financial trends, you have to prepare data that can be learned by your models.
As a general-purpose programming language, Python has evolved to have a versatile battery of tools to support the management of data in your projects. Among these tools, data classes are a useful yet easy-to-grasp technique. …