Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the problems in moving from Python to Crystal language

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "what are the problems of changing from Python to Crystal language". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the problems in moving from Python to Crystal language.

The problem with Python

First, list some of the problems I encountered in Python:

Packaging: this is a problem encountered by most interpreted languages. Packaged into an installable program that includes the entire virtualenv, tools like FPM can make this process very easy, but it still lacks the elegance of a single binary.

Static typing: just like some people go from using C++ to loving it completely, I do miss the type safety I used in C++. This is closely related to compile-time checking, which does help us ensure the quality of our code, even before execution.

Speed: another problem with most interpretive languages. Python is fast enough for many tasks, but it still lags far behind compiled languages.

Tedious: we only have f-strings on Python 3.6.It 's really a relief. However, we still have a very verbose self syntax in classes and structures, with self.var = var everywhere, which may be partially resolved in the data classes of Python 3.7.

Implicit private class member: when I say private, I mean the damn private! As a former C++ programmer, I found that the underscore prefix format of Python's private properties and methods is a little bit. Pervert?:')

Further, I'm not sure I really like where Python is going in several areas, especially in terms of async and typing.

Collaborative process: despite its popularity, the new asynchronous approach in Python feels very unfriendly and difficult to master. Existing code also requires a lot of work before it is non-blocking. I think this situation will improve as more and more libraries are open to use, and as I know and will use more and more new libraries.

Type comments (and mypy): to be honest, type comments are very popular. If they really did something in CPython. Without mainstream support for the main CPython release, the idea of using type annotations as various structures, such as data classes, seems pointless. At the same time, mypy is not yet mainstream, but in the long run, it shows great potential as a Python type check, especially when the-- strict logo is turned on.

I should make it clear that I am still a big fan and supporter of Python and think that it is still one of the current interpreted languages, especially when you consider its amazing ecosystem and maturity.

What am I looking for?

My starting point is Python and Ruby. I often use Ruby where I need it, and I like it very much. Ruby solves several problems that Python has (appropriate private / protected attributes, less verbose syntax, and so on), but still has performance problems and lacks static types.

Therefore, I began to look for new languages with the following characteristics:

Syntax similar to Python and Ruby

Single binary distribution

Compilation, static typing and fast

Object-oriented (Oh class, how much I love you.)

Candidate

The following languages are excluded

GO: no keyword arguments, no exceptions, no classes, no generics, and the horror of naming styles all led me to reject Go (although perhaps this simplicity attracted a lot of people). I actually spent quite some time learning and coding Go, which I think is the most frustrating. After C, languages like C++ have made a lot of progress and provided us with more flexibility, but it feels like Go takes us back to the era of C.

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Elixir: a fascinating functional language, but the lack of object-oriented functionality and the fact that a single binary distribution is not the goal of the language is a bit disappointing for my use case. However, many people on our team use Elixir as the main language for all their new projects and find it excellent in use. Elixir has a rich and reliable tradition, and if you want a functional language, you must consider it.

Rust: it's an interesting language. I spent some time trying to learn it. Really, I just don't think Rust is right for my use case. It's a rather complicated language, and I and a lot of other people don't seem to like it.

Julia: this language is actually for scientific computing, not for my use cases. It also lacks the object-oriented capabilities I want.

Pony: a very attractive language that seems to borrow a lot of Python, but also something I don't like (for example, emphasis on prefix variables, lack of symmetry, etc.). I generally feel that Pony does not agree with my idea that it is not as attractive as other languages, which makes it quite primitive now.

The languages that I am really interested in and hope to study further in the future are:

Nim:Nim is the next language I originally intended to lead, and I hope to spend more time studying it in the future.

Swift: another popular object-oriented language, apart from developing iOS and Mac applications, is definitely worth paying attention to.

But, in the end, I decided to devote myself to learning Crystal!

The reasons are as follows:

Crystal will soon become familiar because most of it follows the syntax of Ruby

It is compiled into a fast, single executable file

The entire standard library is written in Crystal and can be easily read when needed.

It provides a fully object-oriented approach similar to Ruby (including true protected and private members)

Crystal uses static types, but also provides federation (the ability to define variables that can have multiple types)

It provides the ability to develop DSL similar to Ruby (which I've always been interested in)

The binding to the C library is completely native and written in Crystal (similar to ctypes in Python, but better)

Matters needing attention

Crystal is a very young language and still does not have version 1. 0 released. It usually introduces major changes in the version and restricts the library.

However, I intend to use the language only in my personal projects, and I am willing to be an early adopter, because I think the language has enough prospects to be worth using.

experience

Standard library

The whole standard library is very easy to read, and I've been quoting it. The library also seems to have some extensiveness and is a good basic tutorial.

Here is an example of adding an array:

Here is the function to get the file extension:

If you choose to try Crystal, make sure that its source code stays with you; it is very valuable and useful.

Bind to C library

This is amazing!

Here is an example of binding various functions to get user information from the Unix system:

Exception handling

Similar exception handling is provided to Puby and Python:

It's easy to write your own exceptions; you just need to integrate the Exception class.

Import systems and namespaces

These are some tweaks from Python, but because Ruby follows a C++-like approach, it takes me back to the C++ era.

The C++ namespace is the same as you can customize the Ruby/Crystal module. Any library is required to import all the projects it defines, so it always ensures that your entire library is included in the module to avoid namespace pollution.

I was a little worried at first, but I found it easy to build a module from any number of files. However, I have to admit that finding the source of things is more of a challenge.

At this point, I believe you have a deeper understanding of "what are the problems of moving from Python to Crystal language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report