Functions

Study anonymous functions and mapping functions.

Anonymous functions

Using anonymous inline functions in JavaScript React applications is a very common practice. In Python, the Python Enhancement Proposal (PEP) 8 style guide suggests using anonymous functions or lambdas sparingly, and there are more limitations to Python lambdas compared to JavaScript’s anonymous functions. The main restriction is that Python lambdas can only be a single-line statement. Contrarily, JavaScript anonymous functions can be many lines of code. In this case, the only option is to break out the function and create a named Python function to replace it. If the anonymous function is one line, then substituting it with a Python lambda is certainly an option. For example, take this onClick event property that uses an anonymous function to freeze the task parameter into the function call:

onClick={() => this.handleEdit(task)}

This can be directly converted to Python by using a lambda function:

'onClick': lambda: handleEdit(task)

If the anonymous JavaScript function is too complex for a one-line Python lambda, just create a named Python function, and use that function name in place of where the anonymous function used to be. Generally, it is good practice because it usually makes the code easier to read.

Mapping functions

Like anonymous functions, the JavaScript map() function is also regularly used in JavaScript React applications. It takes an iterable like a JavaScript array and applies each element in the array to a supplied function. Python also has a map() function, and while that certainly can be used, the Python preference is to use a list comprehension instead.

In our JavaScript example, a map() function is used to generate the list of task items:

{this.state.taskList.map(this.renderTask)}

If we were to use the Python map() function, we would turn it into this:

map(lambda task: el(ListItem, {'key': task['name'], 'task': task}), taskList)

Using list comprehension

However, it is considered more Pythonic to use a list comprehension, so let’s use one of those. If you are not familiar with or are uncomfortable with list comprehensions, it is a Python construct worth learning about. It’s basically a one-line for loop that creates a new list based on a function.

With list comprehensions, sometimes it’s hard to remember which pieces go where, so one method is to start with the for loop:

for task in taskList:

Then, based on the loop variable (task), figure out what the expression should be to create an item in the new list. In this case, we are creating a list of ListItem components:

el(ListItem, {'key': task['name'], 'task': task})

Finally, since the list comprehension is creating a new list, we wrap those two things together in square brackets to represent the list:

[el(ListItem, {'key': task['name'], 'task': task}) for task in taskList]

We can also filter the items that are included in the final list by using an if clause after the for clause.

Get hands-on with 1200+ tech skills courses.