Library
- In Turkish
- Kütüphane
- Pronunciation
- LY-brer-ee
In short
A library is a collection of ready-made code, such as functions and classes, that a program calls to do common jobs without writing that code itself.
What is a library in programming?
Libraries package solutions that many programs need. Instead of writing your own date handling, you call a date library; instead of building charts from scratch, you use a charting library. Every language also ships a standard library with the basics, such as strings, collections, files and networking, and package managers such as npm, pip and Cargo add hundreds of thousands of third-party ones.
Your code stays in charge when it uses a library: it decides when to call format(date) or axios.get(url), and the library does its job and returns. This is the classic difference from a framework, which provides the overall structure and calls your code at the right moments. It is summed up as: you call a library, a framework calls you.
Using a library means taking on a dependency. Its bugs, security issues, license and future maintenance become partly your concern, and each library you add may pull in further libraries of its own. Popular, well-maintained libraries with clear versions are a safer choice, and lockfiles keep the exact versions reproducible.
A common misconception is that React is a framework. React describes itself as a library for building user interfaces: it handles components and rendering, while routing, data loading and building are left to other libraries or to frameworks such as Next.js that are built on top of it.
Key takeaways
- A library is reusable code your program calls to do common jobs.
- Each language has a standard library; package managers add third-party ones.
- You call a library, while a framework calls your code.
- Every library is a dependency with its own bugs, license and upkeep.
- React is a UI library; Next.js is a framework built on it.
Example
# Standard library: ships with Python
import json
from datetime import date
print(json.dumps({"today": date.today().isoformat()}))
# Third-party library: installed with pip install requests
import requests
response = requests.get("https://api.github.com/repos/python/cpython")
print(response.json()["stargazers_count"])
# Your code decides when to call the library, then carries on.Readers ask
What is the difference between a library and a framework?
With a library, your code is in control and calls the library when it needs something. With a framework, the framework is in control: it defines the structure and calls your code. This is known as inversion of control.
What is a standard library?
The library that comes with a programming language, covering common needs such as text, math, files, dates and networking. Python's "batteries included" standard library is known for being especially large.
What is the difference between a library and a package?
A package is the unit a package manager installs, with a name, version and metadata. It usually contains a library, though it can also contain tools or other files.
Often compared
See also
- FrameworkProgramming Fundamentals, p. 20A framework is a reusable foundation of code, tools, and conventions that provides the structure of an application, so developers only fill in their own logic.
- Package ManagerProgramming Fundamentals, p. 41A package manager is a tool that installs, updates and removes software packages and their dependencies, resolving versions automatically.
- SDKProgramming Fundamentals, p. 51An SDK (software development kit) is a package of tools, libraries, documentation and examples for building software for a particular platform or service.
- APIBackend & APIs, p. 2An API is a set of rules that lets one piece of software request data or actions from another in a predictable, documented way.
- npmWeb Development, p. 32npm is Node.js's default package manager and the world's largest software registry; it downloads a project's dependencies and tracks their versions.
- Semantic VersioningVersion Control, p. 35Semantic versioning is a MAJOR.MINOR.PATCH numbering scheme in which each part signals whether a release breaks compatibility, adds features, or fixes bugs.
Spotted a mistake or something missing on this page?Suggest an edit