Modularizing Code (the Python Way)

Resourceful Code Reuse ā€” by Dmitry Zinoviev (12 / 20)

The Pragmatic Programmers
The Pragmatic Programmers

--

šŸ‘ˆ Arranging Source and Header Files (the C W ay) | TOC | Chapter 2 Reuse Code at Link Time (C Only) šŸ‘‰

The C language was released in 1972. It was originally designed for code reuse, but didnā€™t have the concept of a module: a readily reusable code unit with a declared or easily discoverable external interface. Thatā€™s why we had to jump through hoops (by using ā€œheader filesā€) to construct module-like units.

Modular Languages

The following languages officially support modularization (in the order of their release years; the list isnā€™t complete): IBM RPG (1959), COBOL (1960), PL/I (1964), ALGOL (1965), ML (1973), Modula (1975), Ada (1980), Common Lisp, MATLAB, Objective-C (1984), C++ (1985), Erlang, Object Pascal (1986), Oberon, Perl (1987), Fortran-90, Haskell (1990), Python (1991), Java, Ruby, JavaScript (1995), OCaml (1996), C# (2000), D (2001), BlitzMax, eC (2004), F# (2005), Closure (2007), Go (2009), Rust (2010), Dart, Elixir (2011), Elm (2012), F (2014), NEWP (2015). All but four of them have been developed after C. It would be unfair to blame C for not being modular.

One increasingly popular language that supports modularization by design is Python. Creating a module in Python is ridiculously easy: any Python file is already a module. The name of the module is the name of the file, less the py extension. For example, file jsontool.py hereā€¦

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.