Mastering Python's Higher Functions

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Objectives:

  • Higher order functions

Files Modified: reader.py


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/FunctionsGroup -.-> python/lambda_functions("`Lambda Functions`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") python/AdvancedTopicsGroup -.-> python/iterators("`Iterators`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/with_statement -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/variables_data_types -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/conditional_statements -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/for_loops -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/lists -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/tuples -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/dictionaries -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/function_definition -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/default_arguments -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/lambda_functions -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/importing_modules -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/standard_libraries -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/file_opening_closing -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/iterators -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/data_collections -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/python_shell -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} python/build_in_functions -.-> lab-132505{{"`Mastering Python's Higher Functions`"}} end

Using higher-order functions

At the moment, the reader.py program consists of two core functions, csv_as_dicts() and csv_as_instances(). The code in these two functions is almost identical. For example:

def csv_as_dicts(lines, types, *, headers=None):
    '''
    Convert lines of CSV data into a list of dictionaries
    '''
    records = []
    rows = csv.reader(lines)
    if headers is None:
        headers = next(rows)
    for row in rows:
        record = { name: func(val)
                   for name, func, val in zip(headers, types, row) }
        records.append(record)
    return records

def csv_as_instances(lines, cls, *, headers=None):
    '''
    Convert lines of CSV data into a list of instances
    '''
    records = []
    rows = csv.reader(lines)
    if headers is None:
        headers = next(rows)
    for row in rows:
        record = cls.from_row(row)
        records.append(record)
    return records

Unify the core of these functions into a single function convert_csv() that accepts a user-defined conversion function as an argument. For example:

>>> def make_dict(headers, row):
        return dict(zip(headers, row))

>>> lines = open('portfolio.csv')
>>> convert_csv(lines, make_dict)
[{'name': 'AA', 'shares': '100', 'price': '32.20'}, {'name': 'IBM', 'shares': '50', 'price': '91.10'},
 {'name': 'CAT', 'shares': '150', 'price': '83.44'}, {'name': 'MSFT', 'shares': '200', 'price': '51.23'},
 {'name': 'GE', 'shares': '95', 'price': '40.37'}, {'name': 'MSFT', 'shares': '50', 'price': '65.10'},
 {'name': 'IBM', 'shares': '100', 'price': '70.44'}]
>>>

Rewrite the csv_as_dicts() and csv_as_instances() functions in terms of the new convert_csv() function.

Mapping

One of the most common operations in functional programming is the map() operation that maps a function to the values in a sequence. Python has a built-in map() function that does this. For example:

>>> nums = [1,2,3,4]
>>> squares = map(lambda x: x*x, nums)
>>> for n in squares:
        print(n)

1
4
9
16
>>>

map() produces an iterator so if you want a list, you'll need to create it explicitly:

>>> squares = list(map(lambda x: x*x, nums))
>>> squares
[1, 4, 9, 16]
>>>

Try to use map() in your convert_csv() function.

Summary

Congratulations! You have completed the Higher Order Functions lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like