Sunday 6 January 2013

Lua vs Python or Embedding vs Extending


Another (in)famous comparison!
At least once in your programmer career, you will face the need to add a scripting language to your program. Many famous programs use a scripting language: Unreal, Quake, Emacs, Blender 3D and many games. But, when you decide it and when you start to project your implementation, you face a terrible dilemma: should you make a program with a interpreter or functions for a interpreter? This crossroad is the "embed vs extend". I'll talk about my personal opinion and how this dilemma is equal by Lua vs Python comparison.



Python or Lua? Which one is better?

«If you ask me this, you'll never understand the answer». Maybe it's a too zen statement, but it has a truth. When you decide to use a scripting language on your program, probably you haven't decided yet what's more important: speed? Expressiveness? Clearness? Less verbosity? Python is a complete language, with a huge standard library; it's not very fast and uses much memory than a C/C++ program. Lua is faster and have a small memory footprint, but has a less complete standard library, uses metatables to implement Object Oriented Programming. I assume both are less verbose than C/C++ and both are clear enough.
Conclusion? Probably, I think they have both pros and cons. Rather than ask yourself "which one is better", you should ask yourself "what I need for my next project?"

When Python and when Lua

Now, if I should start a new project without precise design goals about language/technology, I'll use Python. Why?
  1. I like Python syntax
  2. I like Python OOP approach. It's more readable ("class" keyword)
  3. I like Python's huge standard library
Lua is a good language, but I find a bit harder to read it. I tried to realize a roguelike with Lua and Löve framework, but I had difficulties to manage that code (wrote by myself and well commented). It's not probable I shall wrote a big project in Lua. But, if I had a fast core in C/C++, I would like to add some nice features, such as
  1. Customizable GUI
  2. Complex configuration files
  3. Plugins
  4. Complex data files
Yes, you can do it importing the C/C++ core as a Python module. But it's a lot easier to embed a Lua virtual machine and show to it some C/C++ functions. As general rule I could say
if I must create a big program, I'll write it in Python, extending it with C/C++ where I need higher performances. If my program is already done in C/C++, other functions will be provided embedding Lua

Conclusions

I hate the Python vs Lua comparison, because these two tools are suited for different problems. Python is more oriented to create applications and system scripts. Lua is great when you need a easy way to add features and plugins to your program.
Next time you have to choose between embed or extend your program, or if you're thinking if Python is better than Lua, look at your project design. The answer is probably already there.

No comments: