Some Python built-in features that could lead to bugs

Some Python built-in features that could lead to bugs

·

2 min read

Python is regarded as one of the most powerful programming languages. And we all know that "with great power comes great responsibility". Therefore, it is the responsibility of the programmer to ensure they don't run into some of the inbuilt bugs in Python.

Today, I am going to explain and demonstrate how these bugs can cause catastrophes.

1. Arithmetic Operation After Line Break

When the expression in front of a return statement gets too long, the ideal thing to do is to break the line. This is a good practice that could lead to a potential bug as only the expressions directly on the same line as the return gets evaluated. Look at the codes below:

Therefore, it is advisable that you wrap your code in parenthesis after linebreak. No kidding, this error took days to debug - I was really frustrated.

2. 0.2 + 0.1 = 0.30000000000000004

Launch your Python interpreter and write the code below. You'd be surprised that 0.1 + 0.2 != 0.3.

This bug is not limited to Python but is also rampant in many other programming languages. This is due to the fact that there is no perfect representation of 0.1 + 0.2 in binary. Think of it as 1/3 of a decimal - is it 0.333 or 0.33333333.

Now, your banking app is giving users extra cash - not much, but it won't balance out at the end of the year!

To solve this you might need an additional if statement.