Frequently Asked Questions#
Yes and yes! Coconut compiles to Python, so Coconut modules are accessible from Python and Python modules are accessible from Coconut, including the entire Python standard library.
Coconut supports any Python version
>= 2.6 on the
2.x branch or
>= 3.2 on the
3.x branch. In fact, Coconut code is compiled to run the same on every one of those supported versions! See compatible Python versions for more information.
Yes! But only in the backporting direction: Coconut can convert Python 3 to Python 2, but not the other way around. Coconut really can, though, turn Python 3 code into version-independent Python. Coconut will compile Python 3 syntax, built-ins, and even imports to code that will work on any supported Python version (
There a couple of caveats to this, however: Coconut can’t magically make all your other third-party packages version-independent, and some constructs will require a particular
--target to make them work (for a full list, see compatible Python versions).
Since Coconut just compiles to Python, releasing a Coconut package on PyPI is exactly the same as releasing a Python package, with an extra compilation step. Just write your package in Coconut, run
coconut on the source code, and upload the compiled code to PyPI. You can even mix Python and Coconut code, since the compiler will only touch
.coco files. If you want to see an example of a PyPI package written in Coconut, including a Makefile with the exact compiler commands being used, check out bbopt.
Information on every Coconut release is chronicled on the GitHub releases page. There you can find all of the new features and breaking changes introduced in each release.
No problem—just use Coconut’s
recursive_iterator decorator and you should be fine. This is a known Python issue but
recursive_iterator will fix it for you.
Since Coconut syntax is a superset of Python 3 syntax, Coconut supports the same line continuation syntax as Python. That means both backslash line continuation and implied line continuation inside of parentheses, brackets, or braces will all work. Parenthetical continuation is the recommended method, and Coconut even supports an enhanced version of it.
First, you’re going to want a fast compiler, so you should make sure you’re using
cPyparsing. Second, there are two simple things you can do to make Coconut produce faster Python: compile with
--no-tco and compile with a
--target specification for the exact version of Python you want to run your code on. Passing
--target helps Coconut optimize the compiled code for the Python version you want, and, though Tail Call Optimization is useful, it will usually significantly slow down functions that use it, so disabling it will often provide a major performance boost.
How do I use a runtime type checker like
beartype when Coconut seems to compile all my type annotations to strings/comments?#
First, to make sure you get actual type annotations rather than type comments, you’ll need to
--target a Python version that supports the sorts of type annotations you’ll be using (specifically
--target 3.6 should usually do the trick). Second, if you’re using runtime type checking, you’ll need to pass the
--no-wrap argument, which will tell Coconut not to wrap type annotations in strings. When using type annotations for static type checking, wrapping them in strings is preferred, but when using them for runtime type checking, you’ll want to disable it.
When I try to use Coconut on the command line, I get weird unprintable characters and numbers; how do I get rid of them?#
You’re probably seeing color codes while using a terminal that doesn’t support them (e.g. Windows
cmd). Try setting the
COCONUT_USE_COLOR environment variable to
FALSE to get rid of them.
Ease of debugging has long been a problem for all compiled languages, including languages like
C++ that these days we think of as very low-level languages. The solution to this problem has always been the same: line number maps. If you know what line in the compiled code corresponds to what line in the source code, you can easily debug just from the source code, without ever needing to deal with the compiled code at all. In Coconut, this can easily be accomplished by passing the
-l flag, which will add a comment to every line in the compiled code with the number of the corresponding line in the source code. Alternatively,
-k will put in the verbatim source line instead of or in addition to the source line number. Then, if Python raises an error, you’ll be able to see from the snippet of the compiled code that it shows you a comment telling you what line in your source code you need to look at to debug the error.
You’re exactly the person Coconut was built for! Coconut lets you keep doing the thing you do well—write Python—without having to worry about annoyances like version compatibility, while also allowing you to do new cool things you might never have thought were possible before like pattern-matching and lazy evaluation. If you’ve ever used a functional programming language before, you’ll know that functional code is often much simpler, cleaner, and more readable (but not always, which is why Coconut isn’t purely functional). Python is a wonderful imperative language, but when it comes to modern functional programming—which, in Python’s defense, it wasn’t designed for—Python falls short, and Coconut corrects that shortfall.
Definitely! While Coconut is great for functional programming, it also has a bunch of other awesome features as well, including the ability to compile Python 3 code into universal Python code that will run the same on any version. And that’s not even mentioning all of the features like pattern-matching and destructuring assignment with utility extending far beyond just functional programming. That being said, I’d highly recommend you give functional programming a shot, and since Coconut isn’t purely functional, it’s a great introduction to the functional style.
Yes, absolutely! Coconut’s tutorial assumes absolutely no prior knowledge of functional programming, only Python. Because Coconut is not a purely functional programming language, and all valid Python is valid Coconut, Coconut is a great introduction to functional programming. If you learn Coconut, you’ll be able to try out a new functional style of programming without having to abandon all the Python you already know and love.
Maybe. If you know the very basics of Python, and are also very familiar with functional programming, then definitely—Coconut will let you continue to use all your favorite tools of functional programming while you make your way through learning Python. If you’re not very familiar either with Python, or with functional programming, then you may be better making your way through a Python tutorial before you try learning Coconut. That being said, using Coconut to compile your pure Python code might still be very helpful for you, since it will alleviate having to worry about version incompatibility.
The short answer is that Python isn’t purely functional, and all valid Python is valid Coconut. The long answer is that Coconut isn’t purely functional for the same reason Python was never purely imperative—different problems demand different approaches. Coconut is built to be useful, and that means not imposing constraints about what style the programmer is allowed to use. That being said, Coconut is built specifically to work nicely when programming in a functional style, which means if you want to write all your code purely functionally, Coconut will make it a smooth experience, and allow you to have good-looking code to show for it.
I certainly hope not! Unlike most transpiled languages, all valid Python is valid Coconut. Coconut’s goal isn’t to replace Python, but to extend it. If a newbie learns Coconut, it won’t mean they have a harder time learning Python, it’ll mean they already know Python. And not just any Python, the newest and greatest—Python 3. And of course, Coconut is perfectly interoperable with Python, and uses all the same libraries—thus, Coconut can’t split the Python community, because the Coconut community is the Python community.
That’s great! Coconut is completely open-source, and new contributors are always welcome. Check out Coconut’s contributing guidelines for more information.
If you don’t get the reference, the image above is from Monty Python and the Holy Grail, in which the Knights of the Round Table bang Coconuts together to mimic the sound of riding a horse. The name was chosen to reference the fact that Python is named after Monty Python as well.
Evan Hubinger is a full-time AGI safety researcher at the Machine Intelligence Research Institute. He can be reached by asking a question on Coconut’s Gitter chat room, through email at firstname.lastname@example.org, or on LinkedIn.