Simple is better than complex

Photo by Jie on Unsplash

The for loop is a very basic control flow tool of most programming languages. For example, a simple for loop in C looks like the following:

int i;
for (i=0;i<N;i++)
{
//do something
}

Fortunately, when it comes to Python, things become much more convenient. We have many tricks to write much more elegant loops and they do make our lives…


Release the superpower of Python as a dynamic language

The Eval Function in Python: A Powerful but Dangerous Weapon
The Eval Function in Python: A Powerful but Dangerous Weapon
Photo by Gioele Fazzeri on Unsplash

The eval function is a weapon to release the superpower of Python as a dynamic programming language.

Talk is cheap, let’s see it by a simple scenario:

On a normal working day, your boss came over to the software team and said,

“I need you guys to implement a function, which will receive two numbers a and b and a string op. The op, by the way, stands for an arithmetic operator, such as “+”, “-”, “*” and “/”. Your function needs to return the calculated result of a op b.”

Then he added another sentence,

“Don’t waste my time…


Level up the speed of string comparisons

String Interning in Python: A Hidden Gem That Makes Your Code Faster
String Interning in Python: A Hidden Gem That Makes Your Code Faster
Photo by wu yi on Unsplash

The string interning is a very helpful mechanism in Python to make string comparisons much faster. Unfortunately, how it works and how to use it properly are not very intuitive for beginners. If we don’t understand it, sometimes we could be confused about some unexpected results.

For example, the results of the following string comparisons may surprise you:

>>> a = "Yang"
>>> b = "Yang"
>>> a is b
True
>>> c = "Yang Zhou"
>>> d = "Yang Zhou"
>>> c is d
False


This is one of the biggest sources of bugs

Mind the Default Arguments in Python Functions
Mind the Default Arguments in Python Functions
Photo by Boris Smokrovic on Unsplash

Assigning a default value to a keyword argument is very common when defining a Python function. Because it can avoid potential bugs, this practice is strongly recommended if we cannot make sure whether the keyword argument will always receive a value or not.

However, if we don’t use it properly, some new bugs would be introduced and unintended results would appear. This article will show you how bug-prone it is and dive into the reasons under the hood.

A Negative Example of Using Default Arguments

The following code defines a Python function to show my start-of-art tech gadgets:


The cutest syntax sugar in Python

3 Uses of the Ellipsis in Python
3 Uses of the Ellipsis in Python
Photo by Mike Szczepanski on Unsplash

Python has a special built-in singleton object called “Ellipsis”. If we input three dots or the word “Ellipsis” in a Python interactive shell, the results are as following:

>>> ...
Ellipsis
>>> Ellipsis
Ellipsis

This article will introduce the common three scenarios where the ellipsis can be used. After reading, you’ll like this cute singleton object of Python. 😃

1. An Ellipsis Is a Placeholder for Unwritten Code

When designing a new module, we usually define some functions or classes but won’t implement them immediately. Because we only want to determine what…


Understand some confusing results made by Python

3 Facts of the Integer Caching in Python
3 Facts of the Integer Caching in Python
Photo by Ben White on Unsplash

Python has a special feature called integer caching, or integer interning. Sometimes, it may make us confused. For example, if we run the following code on a Python shell, the results are surprising:

>>> a=256
>>> b=256
>>> a is b
True
>>> x=257
>>> y=257
>>> x is y
False

And this is not the weirdest part yet, if we put the same code into a file and run it as a Python script, the results will surprise us again!


Use it properly and skilfully

For-Else: A Weird but Useful Feature in Python
For-Else: A Weird but Useful Feature in Python
Photo by JOSHUA COLEMAN on Unsplash

No matter which programming language we use, we all write “if-else” statements, but how about “for-else” ?

For many languages, such as C, C++ and Java, it’s totally wrong if you use an “else” after a loop. However, Python, as an elegant language, has this weird but useful feature. If we use it properly, our code will become neater and cleaner.

This article will introduce the “for-else” feature in Python and explain how to use it properly by simple examples.

The Basics of the “For-Else” Feature

The “for-else” feature looks strange and hard to understand when a Python developer encounters it for the first time. But…


Feel the flexibility and elegance of Python

5 Ways To Merge Lists in Python
5 Ways To Merge Lists in Python
Photo by Glenn Carstens-Peters on Unsplash

Years ago, when I started to learn Python, I had no idea how flexible and elegant it is. After reading and writing lots of code, I like Python more and more. Because even a common operation can have many different implementations. Merging lists is a good example, and there are at least 5 ways to do this. This article will introduce them and show the tricks under the hood.

1. Add Lists Directly

The simplest way to merge lists in Python is just using the + operator directly, as the following example:


Manage and use Python modules elegantly

3 Key Skills for Handling Modules in Python
3 Key Skills for Handling Modules in Python
Photo by Maik Kleinert on Unsplash

In Python, a module is a file containing Python code and its name has the suffix — .py. Generally speaking, putting related classes or functions into a module and separating the whole program into different modules are good programming practice.

However, when a project becomes larger, handling more and more modules would become difficult.

Fortunately, Python is very flexible and there are many good tricks and practice that can help us manage and use modules elegantly. This article will summary them into three key skills. After reading, handling modules would just be a piece of cake for you. 🍰

1. Import Python Modules Skilfully

When…


The most controversial design pattern

3 Levels of Understanding the Singleton Pattern in Python
3 Levels of Understanding the Singleton Pattern in Python
Photo by Dragos Gontariu on Unsplash

The singleton is a simple but controversial design pattern. For some classes that only one instance of them is needed, such as logging, settings, caches or device drivers related objects, instantiating more than one instances could introduce many unexpected bugs. Therefore, the idea of singleton, which is restricting the special classes’ abilities to only generate at most one instance, is useful for some scenarios.

On the other side, the singleton pattern is considered an anti-pattern by some developers. Because it will introduce unnecessary restrictions and global variables. Not to mention it reduces the readability of code.

Although controversial, it is…

Yang Zhou

Software Engineer | Investor | Entrepreneur yangzhou1993.medium.com/follow

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store