Serdar Yegulalp
Senior Writer

A first look at the Mojo language

feature
Jun 7, 20236 mins

Mojo aims to be as easy to use as Python, but as powerful and fast as Rust. Here's a first look at Python's newest challenger.

fighter boxer competition boxing glove best fight  totokita getty
Credit: TotoKita / Getty

The newly unveiled Mojo language is being promoted as the best of multiple worlds: the ease of use and clear syntax of Python, with the speed and memory safety of Rust.

Those are bold claims, and since Mojo is still in the very early stages of development, it will be some time before users can see for themselves how the language lives up to them. But Mojoโ€™s originatorโ€”a company named Modularโ€”has provided early access to an online playground: a Jupyter Notebook environment where users can run Mojo code and learn about the languageโ€™s features and behavior.

Because Mojo isnโ€™t available yet as an end-user download, this first look focuses on what Mojo is like as a language. Weโ€™ll examine how it resembles Python, how itโ€™s different, and what it has to offer for a programmer familiar with Python or other languages.

Mojo language basics

Mojo can be described as a โ€œsupersetโ€ of Python. Programs written in Python are valid Mojo programs, although some Python behaviors havenโ€™t yet been implemented. Some examples of Python behavior that you wonโ€™t currently find in Python are keyword arguments for functions, the global keyword, and list and dict comprehensions. Itโ€™s also possible to use the actual Python runtime for working with existing Python modules, although there is a performance cost.

When Mojo introduces new syntax, itโ€™s for system-level programming features, chiefly manual memory handling. In other words, you can write Python code (or something almost exactly like it) for casual use cases, then use Mojo for more advanced, performance-intensive programming scenarios. In both cases, you can draw on existing Python libraries, but the performance cost is higher.

Mojoโ€™s other big difference from Python is that Mojoโ€™s not interpreted through a runtime, as Python is. Mojo is compiled ahead-of-time to machine-native code, using the LLVM toolchain. To that end, the best performance comes from using features specific to Mojo. Python features are likely to come at the cost of emulating Pythonโ€™s dynamic behaviors, which are inherently slowโ€”or again, by just using the Python runtime.

Mojo vs. Python syntax

Many of Mojoโ€™s native language features do one of two things. Theyโ€™re either entirely new features not found in Python at all, or expansions of a Python feature that make it more performant, although with less of Pythonโ€™s dynamism.

For instance, in Python, there is no way to formally declare a variable reference immutable at runtime, although type hinting and other mechanisms can emulate this at edit time. In Mojo, you can use the keywords let and var to declare Mojo-specific variables, in much the same way as you would in Rust. The let keyword indicates a variable is immutable; var indicates itโ€™s mutable. Those restrictions are enforced at compile time, so programs that try to mutate immutable references wonโ€™t even compile.

Mojo syntax IDG

Mojoโ€™s syntax strongly resembles Python, but offers new keywords to enable Mojo-specific features like variable behaviors.

Mojo also has its own struct keyword that stands in contrast to Pythonโ€™s class. Classes are just Python classes, with all the dynamic (and slow) behaviors youโ€™d expect. The struct types, though, are more like their C/C++ and Rust counterparts, with fixed layouts determined at compile time but optimized for machine-native speed.

Another Mojo keyword designed to distinguish Mojoโ€™s behaviors from Pythonโ€™s is fn. If you use def to define a function, you get a Python function, with all the dynamism associated with such things. The fn keyword defines a function, as well, but as a Mojo function. This means arguments are immutable by default and must be explicitly typed, and that all local variables must be declared (among other things).

The Modular Playground

If you want to find out what itโ€™s like to work with Mojo right now, youโ€™ll need to take a number and get in line. Modular offers early access to Mojo through the Modular Playground, a web-based Jupyter Notebook environment that runs on Modularโ€™s server. Right now, Mojo doesnโ€™t have a runtime available for download on oneโ€™s own system. On the plus side, this means you can run Mojo by way of any computer with a web browser.

The Mojo online environment comes with a few example notebooks, with detailed inline notes about using Mojo for certain tasks. One of the examples is a common programmerโ€™s demo, plotting a Mandelbrot set algorithm. At a glance, the code closely resembles Python. Even the new Mojo-specific keywords integrate well with existing Python syntax, so you can run your eye down the code and get a general idea of whatโ€™s happening.

That demo also shows how to use existing Python modules as part of a Mojo workflow. The actual math is handled in Mojo, then rendered into a numpy-style array and passed to matplotlib, a C library, for rendering. In other words, while this demo makes use of legacy Python libraries, everything performance-sensitive is in either Mojo or C, so the Python parts arenโ€™t a significant bottleneckโ€”which is more or less as it would be when Pythonโ€™s used well.

Modular Playground for Mojo IDG

The Mojo playground in action, running a Mandelbrot plot demo. The code used to generate this demo is a mix of native Mojo code and Python libraries, invoked through Mojoโ€™s interface to the Python runtime.

The notebook demos also give examples of how Mojo code can be accelerated via parallelism, vectorizing, and โ€œtilingโ€ (increasing cache locality for operations). One of the demos, a 128ร—128 matrix multiplication demo, yielded a claimed 17-times speedup over Python (using the Python runtime in the Mojo playground) by simply running as-is with no special modification. Mojo added 1866x speedup by adding type annotations, 8500x speedup by adding vectorized operations, and 15000x speedup by adding parallelization.

Again, the best way to verify these claims would be by having Mojo available locally, but itโ€™s worth experimenting with both the Python runtime and the Mojo compiler in the same code side by side to see what happens.

Could Mojo replace Python?

The very first public pitches for Mojo made a case for it as a language for data science and machine learning. Those two subjects make up a big part of Pythonโ€™s modern use caseโ€”not because Python itself is fast, but because it provides a convenient programmatic interface for fast things that are hard to use as-is.

Mojoโ€™s clearly meant to provide a fast-by-default version of that use case, where you donโ€™t have to reach for external libraries to make things fast. What Mojo isnโ€™t aiming for, at least not this early on, is Pythonโ€™s broader set of use cases: web back ends, process automation, and so on. That may come later, when Mojo is more complete and has a better collection of third-party libraries, but it clearly isnโ€™t the first order of business.

Even if Mojo is faster by default, itโ€™ll be tough to displace Pythonโ€™s presence in machine learning and data science. Pythonโ€™s user community, existing culture of software, and convenience all make it a mainstay of those fields. Mojo would have to do more than just be fast to replace Python for that work. Still, itโ€™ll be interesting to see how Mojo continues to develop along both its Python-compatible and high-speed use-case paths.

Serdar Yegulalp

Serdar Yegulalp is a senior writer at InfoWorld. A veteran technology journalist, Serdar has been writing about computers, operating systems, databases, programming, and other information technology topics for 30 years. Before joining InfoWorld in 2013, Serdar wrote for Windows Magazine, InformationWeek, Byte, and a slew of other publications. At InfoWorld, Serdar has covered software development, devops, containerization, machine learning, and artificial intelligence, winning several B2B journalism awards including a 2024 Neal Award and a 2025 Azbee Award for best instructional content and best how-to article, respectively. He currently focuses on software development tools and technologies and major programming languages including Python, Rust, Go, Zig, and Wasm. Tune into his weekly Dev with Serdar videos for programming tips and techniques and close looks at programming libraries and tools.

More from this author