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
As we knew, the
is operator is used to compare whether two variables refer…
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:
There are no other ways to write a for loop more elegantly in C. For a complex scenario, we usually need to write ugly nested loops or define lots of assistant variables (like the
i in the above code).
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…
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.
The following code defines a Python function to show my start-of-art tech gadgets:
Nothing seems wrong of the above code, and…
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:
This simple object seems inconspicuous, but if we use it properly, it can make our lives easier.
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. 😃
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…
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 is b
>>> x is y
Why 256 is 256 but 257 is not 257? Anything wrong?
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!
We put all the code into the…
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 “for-else” feature looks strange and hard to understand when a Python developer encounters it for the first time. But…
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.
The simplest way to merge lists in Python is just using the
+ operator directly, as the following example:
+= operator also supports lists. But, things become interesting…
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. 🍰
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…
We all know that Python is an elegant programming language. But everything has weaknesses. Sometimes Python is not as elegant as it should be.
For example, when we need to break out of nested loops as follows:
for a in list_a:
for b in list_b:
break keyword can only help us break out of the inner-most loop. Can we directly break out of the two nested loops at once? Are there some built-in keywords or tricks for this in Python?
Unfortunately, there is no built-in support for this operation.
As an old saying goes, “comparison is…