avatarMaks Lunev

Summarize

Face the 5 main weaknesses of Python and learn how to deal with them

Discover the weak points of Python and find the solutions to the problems behind them to unlock the full potential of the language

Image by jcomp on Freepik

Introduction

As Python developers, we all know what are the strengths of Python. It is the most popular programming language, it has the biggest community, it has many libraries and modules, it is one of the easiest programming languages, it is user-friendly, etc…

Yes, Python is a great language but it’s not perfect. Like every other programming language, it has strengths but it also has a lot of weaknesses which can be a problem. And that’s why today we will talk about these weaknesses and try to find effective solutions to each one of Python’s main problems.

It’s always important to find our weaknesses or in this case the weaknesses of the programming language that we are working with and fight them. By overcoming them, we’ll become better individuals and we’ll unlock the full potential of the programming language that we use every day.

Speed

You probably know that Python is relatively slow compared to other programming languages. The main reason why Python is so slow is because the execution of the code is done with an interpreter and not with a compiler which simply means that the code is executed line by line and not simultaneously. It needs a lot of computational power to run.

During runtime, it executes many background tasks related to common programming behaviors. For example, the variable data types are one of those operations. We don’t need to specify them in our code which makes the language easy to understand and write but on the other hand, it slows down the execution process. Other factors such as the number of lines of code and the complexity of your program contribute to the slowness of Python too.

SOLUTIONS

  • Optimize your code: Use the “time” module to compare operations and find which ones take less time to execute. Use techniques like list comprehension, generator expressions, and local variables when it’s possible of course.
  • Use multiprocessing: Familiarize yourself with the “multiprocessing” module, which helps you perform multiple processes simultaneously as the name tells.
  • Reduce the number of lines of code: Double-check your program to be sure that you didn’t write any unnecessary lines of code. An easy way to reduce them is to leverage libraries and modules but be careful because sometimes, the use of specific modules and libraries makes your program slower and adds more lines of code rather than reducing their number.

Mobile development

Python is the leader in back-end web development, machine learning, and desktop application development, but when it comes to mobile development, sadly, it is not his strength. Languages like Swift, Kotlin, and Java outperform Python in this field.

The main reason is that there aren’t that many libraries and tools to allow developers to build good-looking and competitive mobile applications. Since mobile development isn’t the number one target of Python, the provided tools aren’t necessary to build one-of-a-kind mobile apps.

The second reason is that Python has very limited access to mobile-specific features such as camera, geolocation, push notifications, digital keyboard, and more. You will encounter some difficulties distributing your app to the stores as well since they have restrictions and guidelines that favor mobile programming languages like Kotlin and Swift. Yet it’s still possible.

SOLUTIONS

Master Kivy: Kivy is an open-source, cross-platform Python library that allows you to create mobile muti-touch applications that you can distribute on mobile stores such as Google Play and App Store. There isn’t a better choice for Python mobile development, you just won’t survive without it. It may not possess absolutely all the existing features that you encounter in mobile development but it has pretty much everything you need to get your app done and distributed. Another good library is Beeware.

Use third-party libraries for mobile-specific features: About 20 seconds ago, I mentioned that access to mobile-specific features in Python is limited. If there is a feature that you need for your app that Kivy doesn’t have, consider helping yourself with some third-party Python libraries such as Plyer. This kind of library can give you access to more hardware features on different platforms.

“Hack” things up: This sounds very confusing, right? There isn’t always a specific library, function, or module that performs an operation in an easy way for you, there aren’t that many built-in or third-party tools in Python to cover all the needs and problems encountered in programming. But this doesn’t mean that you can’t “invent” a way to counter a problem that hasn’t a tool designed to solve it. For example, in Tkinter, you can’t create rounded buttons, you just can’t, Tkinter doesn’t possess such a feature, however, it’s possible to create an image of a button that is round and then use the image you’ve created in your program. You have a rounded button that looks the way you like. You can try to apply this if you struggle with a missing feature.

Memory

Python has a high memory consumption and it’s not wise to use it for extremely intensive memory tasks. It is a dynamically typed language, so the main reason is the flexibility of the data types. Sometimes during the execution of the program, variables change their data types which result in a higher memory consumption.

The fact that everything in Python is seen as an object and an object consumes more memory contributes to the memory consumption. Garbage collection and the extensive libraries and built-in features are important factors as well. But don’t worry, you’ll have memory problems only if you work on long and complex projects, not small ones obviously. But let’s see some ways in which you can face this even if your project is long and complex:

SOLUTIONS

Favor generators and iterators: By generating and iterating over a large dataset or data structure, you process data piece by piece instead of processing all the data from a long dataset at once. This approach is very important when you have to deal with the processing of very large amounts of data in your code.

Optimize data structures: Data structures like lists, tuples, dictionaries, sets, and others are used to store data. Some data structures consume less memory than others. For example, tuples occupy less memory than lists. Review your code and change data structures that are too memory-consuming when it’s possible of course.

Always close your resources: If you forget to close a file or a database connection, your memory consumption will increase. So, always close your files and other resources when you are done with them. For those who often forget to close their files, there is a very cool feature called context manager which closes the file automatically.

Runtime errors

There are a lot of errors that occur only when the program is running. The reason once more is that Python is dynamically typed. The variables can change their data types during the execution of the program and we all know that when the data type of a variable is changed, there is a high chance that an error occurs.

Of course, there are runtime errors in other programming languages too, but there are more frequent in Python. This isn’t as important as the previous disadvantages but it’s pretty annoying, right? The good thing is that it isn’t that hard to counter this. Let’s see how:

SOLUTIONS

Testing: If you want to avoid bug accumulation, the best strategy is to test your program more often. You can test it after each bloc of 4–5 lines for example. This will let you keep track of the process and discover runtime errors more quickly. By testing your program more often, you generally discover errors one by one and not 3 or 4 at once which makes you concentrate on one error and the result is that you will fix errors faster.

Exception handling: I think this is the best solution to catch runtime errors. There are many built-in exceptions handling various situations. By using “try, except, else, finally” blocs, you can easily handle data type errors which are the main cause of runtime errors. Even if you are in a scenario where there isn’t a Python built-in exception for your case, you can create your own custom exception. If you don’t know how I have an article that covers that.

Database access

Database access in Python is limited. That’s because Python does not have a standardized database API like Java’s JDBC. Instead, it relies on different database-specific libraries and modules, each with its own API and syntax. This makes our job as programmers difficult because we have to adapt to multiple syntaxes.

Python’s object-oriented nature, dynamic typing, and interpreted execution have some impact on database operations. The query optimization is a little bit difficult as well.

SOLUTIONS

Master SQLAlchemy: This is a powerful library that provides a unified way of working with multiple databases. It possesses an Object-Relational Mapping (ORM) layer, this complex thing means that it is more simple to write SQL queries. SQLAlchemy has many extensive features, it is flexible and compatible with many database systems.

Leverage the capabilities of your DBMS: If you are working with databases in Python, then you must already be familiar with the SQL language. If so, then do the bigger part of the work using your SQL DBMS (Database Management System) and the smaller part using Python. For example, you can create all the tables and define all the primary keys directly from your DBMS. You will then fill the tables with values using Python. By doing this, you will not only avoid most of the problems related to Python’s lack of database access and absence of features but you will also save some memory space.

Conclusion

Every problem has its solution. Today we discovered the main weaknesses of Python and we searched for ways to face these weaknesses and the problems behind them. Don’t feel discouraged after reading all of this. Python is an amazing language that has a lot more strengths than weaknesses. Python is easy to use, it has many extensive libraries, an extremely big community, code readability, easy understanding, and more.

Yes, it has weaknesses too, it’s inevitable, but I think that we should face all these problems and fight to find a way to counter them and use Python regardless of those weaknesses rather than just giving up and ignoring them. What’s your opinion about this?

Thank you all for reading this!

Resources

More content at PlainEnglish.io.

Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

Python
Python Programming
Tips And Tricks
Data Science
Database
Recommended from ReadMedium