Categories
Technology

What’s New in Python 3.10?

 

Python 3.10 is the latest version of Python and has been released in its first beta version. With the release of the beta version, the set of features for Python 3.10 has been finalized and the audacious Python developers are encouraged to examine their code with their latest builds. There are some interesting additions in Python which we are going to cover – structural pattern matching, more precise error reporting, parameter specification variables and other major changes.

Here’s a synopsis of all the major new features in Python 3.10 and how they can help your code.

Structural Pattern Matching: There were many failed attempts to add a case-like or switch syntax to Python and structural pattern matching is the outcome of such attempts. It enables you to match variables with one set of possible values with case/switch in other languages. It also enables you to match with the patterns of values like an object to set a certain value with a certain property. This enhances the range of possibilities to a great extent and makes it possible to write a code that quickly includes various scenarios. One of the examples is shown below –

command = input()

match command.split():

case [“quit”]:

quit()

case [“load”, filename]:

load_from(filename)

case [“save”, filename]:

save_to(filename)

case _:

print (f”Command ‘{command}’ not understood”)

Precise Error Reporting: The error reporting with Python was dependent on the impulse of the parser for so long. Python 3.9 has unfolded an entirely new parser that is more robust, faster and easier for the team of Python to maintain and be less corrupted with internal hacks. The biggest advantage of the new parser is that it offers the developers far more useful error messages which are also more precise. In the 3.8 version of Python, the following code would generate a syntax error.

print (“Hello”

print (“What’s going on?”)

File “.\test.py”, line 2

print (“What’s going on?”)

^

SyntaxError: invalid syntax

The error message is not so helpful as the real problem lies in an earlier line. The latest version of Python generates a much more useful error.

File “.\test.py”, line 1

print (“Hello”

^

SyntaxError: ‘(‘ was never closed

In this vein, most of the errors produced by the parser have been improved, they not only deliver accurate information about the error but more precisely talk about where the error occurred.

Parameter Specification Variables: The typing module of Python is used to illustrate the code with type information and lets you outline the type of callable like a function. That type of information cannot be spread across all the callable which makes it difficult to illustrate things like function decorators. There are two new additions to typing typing.Concatenate and typing.ParamSpec makes it possible to explain callable with more abridged type definition information. Here is an example of this new feature –

from typing import Awaitable, Callable, TypeVar

R = TypeVar(“R”)

def add_logging(f: Callable[…, R]) -> Callable[…, Awaitable[R]]:

async def inner(*args: object, **kwargs: object) -> R:

await log_to_database()

return f(*args, **kwargs)

return inner

@add_logging

def takes_int_str(x: int, y: str) -> int:

return x + 7

await takes_int_str(1, “A”)

await takes_int_str(“B”, 2) # fails at runtime

As it is not possible to specify the liner with proper details regarding what kind of types are being passed to the functions that are handled by the decorator, the liner is not able to catch the invalid types in the second occurrence of takes_int_str. Below is shown how the code would look with the latest parameter specification variable syntax.

from typing import Awaitable, Callable, ParamSpec, TypeVar

P = ParamSpec(“P”)

R = TypeVar(“R”)

def add_logging(f: Callable[P, R]) -> Callable[P, Awaitable[R]]:

async def inner(*args: P.args, **kwargs: P.kwargs) -> R:

await log_to_database()

return f(*args, **kwargs)

return inner

@add_logging

def takes_int_str(x: int, y: str) -> int:

return x + 7

await takes_int_str(1, “A”) # Accepted

await takes_int_str(“B”, 2) # Correctly rejected by the type checker

ParamSpec enables us to indicate where to capture positional and keyword arguments. Concatenate can be used to pinpoint how arguments are removed or added.

Other Major Changes:

  • Instead of Union [X, Y], Union types can now be expressed as X|Y for brevity.
  • The built-in zip has now a strict keyword that puts together the results of various iterables. When True is set, the zip raises an exception if any one of the iterable is used up before the others.
  • Parenthetical syntax and multi-line now support
  • Variables are now stated as type aliases, they enable forward references, the better difference between type declaration in scopes and more robust errors which involve types.
  • To build CPython, SSL 1.1.1 or a newer version is now required which revamps one of the key dependencies of CPython.

Final Words

These were some of the important new features introduced with Python 3.10. The full release of this version is expected in October 2021 between which the Python developers will be working on enhancing what has been added. It is still in the alpha version and still far away from being fully tested and ready to use.

About Digital Crafters

Being a leading Python web and software development company, we deliver best-in-class web applications using outstanding Python development frameworks, programming language, Databases like Django and other advanced tools. The dedicated full-stack Python developers with us work exceptionally to give amazing results for the client’s Python application development project. We perceive the business of the client as our own and transform the out of the box ideas into profitable web applications and websites.

Write to us at info@digitalcrafters.tech for more details about our services.

Categories
Technology

Node.js vs Python – Choosing the Best Backend Framework

 

Choosing the right programming language for a new project is one of the most difficult decisions programmers make. In the world of programming, there is no such thing as a master of all trades and every new project comes with a unique problem. Different programming languages have their own strengths and weaknesses which make them fit for some situations but not all. Node.JS and Python are in the group of the most popular technologies for backend development. We will compare these two technologies to figure out which works better in which case. 

What are Python and Node.JS?

Python is the most popular language for Machine Learning and GitHub’s second most popular language. It is an object-oriented programming language that is dynamically typed and supports multiple programming models. It is used to create applications for web, desktop and mobile and comes with an all-inclusive collection of libraries and packages to smoothen the process of development. 

Node.JS is majorly used as a backend framework and is a JavaScript runtime environment created on Google Chrome’s V8 JavaScript engine which is the dominant contributor to enhancing the efficiency of Node.JS code. It is used to build scalable and efficient web applications that run on JavaScript and can be used for frontend and backend development as well. 

Detailed Comparison of Python and Node.JS Development

To give a better understanding of both the competitors, here is a detailed comparison with some of the pre-defined factors. 

Speed and Performance: In Python web development, the processes requests in a single flow whereas, in Node.JS, multithreading is possible. Django makes it easy to handle high loads up to a limit but this does not make it a great option for mobile applications.

With the V8 engine, the speed and efficiency in Node.JS development are vastly enhanced, it interprets JavaScript code to machine language enabling it to deliver excellent performance. While developing real-time web solutions, Node.JS should be the first option where updates are also generated and shared. 

Trending Technologies: In the smart technology era, Python development service is the go-to choice for cutting edge technologies like Data Science, Machine Learning and IoT. For ML and IoT, there are various tools, libraries and variants of Python along with experts in the community.

Node.JS possesses the capability to build IoT devices but is more popular than Python for developing web applications including real-time communication. Because of JavaScript’s popularity, Node.JS development is being increasingly used as a server-side framework. 

Architecture: Python requires special tools to provide support for asynchronous programming. The Node.JS architecture includes a single thread event loop which enables it to handle thousands of concurrent connections when paired with the non-blocking nature of Node.JS. This makes Node.JS the best option for real-time web applications. 

Syntax: With Python’s syntax, it is possible to write fewer lines of code to get more. It is free of curly brackets which makes it easier to understand and debug. It is a beginner-friendly programming language and most people can read the code easily with a bit of technical knowledge.

The Node.JS syntax is largely similar to the browser’s JavaScript syntax and with prior knowledge of JavaScript, one should face no challenges while working with Node.JS. 

Universality: Python comes bundled with macOS and Linux and can be used for frontend and backend cross-platform development. It is a powerful programming language for desktop and web development but is an impractical choice for mobile development.

Node.JS is also commonly used for the frontend and backend development of web applications. It also reduces development costs and efforts creating a variety of cross-platform applications on mobile, web, IoT and cloud. 

Scalability: Python development lacks scalability as the runtime interpretation of Python code makes it a slower programming language. It also lacks support for multi-threading as the internal lock mechanism prevents it from running multiple tasks together. These limitations can be reduced by using Python implementations like Jython or CPython.

Node.JS development is naturally scalable as it is built into the runtime environment and includes a cluster module that possesses the capability of handling the full power of your machine. It also allows for horizontal and vertical scaling of web applications by adding new nodes for horizontal scaling and additional resources for vertical scaling. 

Extensibility: Python can be easily extended with several frameworks like Flask, Django, web2py and more for web-only and full-stack development. The features can also be extended for the C/C++ programming language with an API given in the C source file.

Node.JS has a pool of frameworks for extending its features like Loopback.JS, Derby.JS, Koa.JS, Hapi.JS and many more which enable the developers for faster application development. 

Error Handling: In the case of Python development, it is easier for developers to find and debug errors because of its compact and readable syntax. Python has an upper edge when compared to Node.JS for better and easier error handling.

Node.JS development is also capable of finding and debugging errors and includes superior exception handling options. It can handle the error at any point in the lifecycle of an application. 

Final Words

Node.JS and Python, both are very powerful options for programmers to develop web applications. Choosing between the two depends on two important factors – the skill of the developer and the purpose of the project. They both offer some advantages over the other but these will never dominate the requirements of the project as there is always a solution for the shortcomings. It also depends on how comfortable your team can work on any one of the technologies. 

About Digital Crafters

Digital Crafters is a well-known Node.JS application development company with hands-on experience in creating feature-rich and high-performance Node.JS solutions for clients across the globe. Being a leading Python web and software development company, we deliver best-in-class web applications using outstanding Python development frameworks, programming language, Databases like Django and other advanced tools. The dedicated full-stack Python developers with us work exceptionally to give amazing results for the client’s Python application development project. We perceive the business of the client as our own and transform the out of the box ideas into profitable web applications and websites. Write to us at info@digitalcrafters.tech for a free consultation.