7 Python basics for JavaScript engineers

sdotson

Stuart Dotson

Posted on March 16, 2020

7 Python basics for JavaScript engineers

I’ll be honest. My main expertise and experience is in JavaScript with Node and frontend frameworks like React. I took a Python Introduction to Computer Science course through EdX a few years back and have only really needed to use it sporadically since then. That all changed two weeks ago when a new project started at work. My team was tasked with rapidly getting ramped up on Python and writing a new back-end service in the language.

Here are 7 things I’ve learned so far writing in the Python language every day:

1. Python version management

I feel like it’s less common for folks to alternate between different versions of Node these days but the tool to do that in nvm. Python has competing solutions for this problem. Two of those are conda and pyenv with pyenv-virtualenv

pyenv allows you to use different versions of python and and pyenv-virtualenv allows you to sandbox different environments with those different python versions so they don’t conflict. For a while pyenv and pyenv-virtualenv were working fine until one day I kept getting really cumbersome openssl issues preventing me from installing dependencies with pip.

I switched to conda. conda is both an environment and package manager. A practice I learned recently was to create a new environment for each project you are working on. So far no issues. 

2. Python dependency management

In JavaScript, you are typically using a NPM package registry, using yarn or npm to download those dependencies to your local machine, and the package.json file to list the dependencies that need to be downloaded.

In Python the public package registry is the Python Package Index (PyPI)(https://pypi.org/), you use pip to download dependencies, and the list of dependencies are in requirements.txt and requirements_test.txt.

3. Python property/method reference

You can refer to properties in many different ways in JavaScript and you can use any of them regardless of context. You can refer to the assign property of Object with Object.assign or Object["assign"], the only real rule being that string property names are generally referred to with dot notation and number property names must use the bracket notation.

In Python dot notation and bracket notation communicates the relationship of the property to the object. Classes must use the dot notation and dicts must use the bracket notation. Don’t worry, Python will let you know when you make a mistake with AttributeError if you try to use dot notation with a dict and object is not subscriptable when you use bracket notation for a class property. Those errors are forever burned into my mind.

4. Python async/await

Finally something familiar to JavaScript folks and more or less works the same. In JavaScript async/await works out of the box assuming you’re using a recent enough version of Node. In Python you need to import in asyncio and make sure the async/await happens within the context of a created event loop. JavaScript also doesn’t allow you to await in a global context. This might change with the proposal for global await but for now you have to use await within the context of a async function.

5. Python types

Most of the JavaScript I write these days is with TypeScript. Python has the typing standard library package which can be extended with pydantic.

6. Python case

JavaScript favors camelCase. Python favors snake_case. I suppose you see a bit of both in out in the wild in terms of API JSON formats. 

One way Python tackles this is you can use pydantic for your type models so that you can use the idiomatic snake_case in your Python service yet use camelCase for the output when you JSON serialize. Here’s one tutorial.

7. Python anonymous functions

In JavaScript, anonymous functions are declared like so: () => {}. You can assign this to a var const myFunc = () => {} or even declare and invoke in place as an immediately invoked anonymous function (() => {})().

In Python the equivalent is a lambda function:

def add_one = lambda x: x + 1
Enter fullscreen mode Exit fullscreen mode

In the above example we are declaring a function that accepts argument x and returns x + 1. You might be wandering why on Earth this is called a “lamba function”. According to this informative article from Real Python, lamba functions have roots in something called “lambda abstractions” that were developed as a part of “lambda calculus” in the 1930s, which is one of the foundational building blocks of functional programming..

Conclusion

I hope to write a few more of these posts as long as I’m continuing to write Python at work and learning things. I’m enjoying it quite a bit, in a way that feels different than when I was in a similar situation learning Go for another service at work some time ago.

💖 💪 🙅 🚩
sdotson
Stuart Dotson

Posted on March 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related