This is not a guide or step by step preparation for a tutorial. Rather more of a kind of experience amalgamated with a gesture of opinions.
Python comes pre loaded with Linux and Mac, but for windows we may have to install it. Mostly Python2 will be the default version. Days of Python2 is gonna fade eventually, all the recent projects are using Python3. Incase of starting a new project, I would hardly recommend Python2 unless there is a serious dependency with legacy code or some library. Python3 might not come out of the box, but could be easily installed.
Well…. am not sure of any operating system that comes NodeJS pre installed, I mean till date. But installation is straight forward in all platforms. There are executable for Windows and brew/apt/yup/… packages for mac/linux.
Starting a REST api project
So for an accelerated development, we could choose flask or dJango(read as jango) to write apis. Flask is a customizable library with less opinionated view. We could re draw the architecture of the project as we need. Now this comes with its on responsibility too. Where as dJango is a sort of frame work. Architectural changes that we could make to the project is limited. But the architecture of dJango is done by an amazing engineers and is rock solid. Needless to say, instagram is one of the largest dJango deployment.
Express is the first word everyone yells when it comes to REST in NodeJS. Express is a light weight and good framework. But there are options like hapi, restify (Netflix uses this) and loopback. Even without any framework we could create REST endpoints, but that may look like re inventing the wheel.
Python uses pip and NodeJS uses npm, although there is a stronger sibling yarn. One thing to notice about Python dependency management is, all the external dependencies installed using pip goes to the system binaries of Python. I mean, all the dependencies will be saved where Python is installed. Considering the age of Python, this seems to be an obvious choice that they made during those days. The problem arises when we need to run multiple application written in Python which uses different versions of same library. And with virtualenv this problem is no more NPHard.
NodeJS dependency management is more clean. All the dependencies mentioned in the
package.json file goes to the folder
node_modules which resides in the project’s root directory. So multiple project using multiple versions of a specific library is not an issue. Python needs the Python binaries along with dependencies, where as NodeJS doesn’t need it. And to export a module from a file an explicit export statement is needed in NodeJS, where as its not needed in Python. And the import statement is always relative to the file in NodeJS, where as its absolute in Python.
Python uses synchronous coding style, which means the second statement will wait till the first statement completes execution. On the other hand, NodeJS by default is asynchronous. Which means, if we hit any process which uses i/o, that goes to background and next statement starts executing. And this is the reason why NodeJS is good for an i/o or data intensive back end.
Async code usually comes under the cost of readability. But the recent ES6
JSON and marshmallow
- Functions can be nested.
- Functions can be passed as parameter to another function.
- Function could be returned from another functions.
- Functions could be stored into the data type.
This is one awesome thing I felt in Python. If any one who is not aware of decorator, let us take an example to explore it
@foo def method(params): pass
foo is the decorator called here.
Here the function
methodwill be passed as an argument to the decorator function
foo. The function
methodwill be called inside the function
For step by step guide to function decorators in Python, refer this.
Well in the context of a data intensive restful back end service, NodeJS might perform sound. Especially the new v8 improvements coming each day makes NodeJS stronger and quicker. Needless to say it might hang a bit when it comes to computational intensive stuffs. This is why because, NodeJS runs on a single thread called event loop. Whenever an i/o operation occurs, event loop with the help of libuv library sends the tasks to newly spawned thread. So new thread spawning happens only for an i/o task. Am not talking about the experimental implementation of workers and clustering here. Those works in a different way.
Python is not as fast as NodeJS, but could do complex thing in simple way. And for computational intensive tasks Python could go better than NodeJS. On top of that, using cython, a
C dialect of Python gives much performance improvement over Python for computational intensive tasks.
Availability of libraries
npm we could get a library for almost any purpose.
NodeJS and Python comes with debuggers. Which means rather than printing each line to test output, you could put breakpoint and stop execution flow. One easy way to work with debuggers is using vscode or JetBrains ide. Since NodeJS runs on top of v8, it could be directly debugged using Chrome.
Both Python and NodeJS is great tool for back end. Both are growing exponentially. Choosing the right platform depends upon several aspects and tradeoffs. Few of them worth mentioning are
Go with Python if you are building
- Computational intensive.
- You have good no of Python developers in the organization.
Go with NodeJS if you are building
- Data Intensive.