Skip to content

Repository files navigation

Welcome to fastcore

Note

fastcore v2

In July 2026 we released fastcore v2, which removes or relocates a number of APIs that had accumulated better alternatives. If you use from fastcore.utils import * (or fastcore.all), most of these changes won’t affect you. The breaking changes: Param is gone from fastcore.script; use plain type annotations with docments, or typing.Annotated[type, "help"], optionally with a dict of argparse arguments for advanced features. L’s starmap, starfilter, and the other star*/rstar* methods are replaced by the star and rstar function adapters, which compose with every L method (e.g. t.map(star(f))); relatedly, spread is replaced by star, and dspread is renamed to dstar. Async helpers now live in the new fastcore.aio module: run_sync, iter_sync, and ctx_sync moved there from net, and maybe_await, then, mapa, acache, reawaitable, is_async_callable, and the other async utilities moved there from xtras. Config and the config file functions moved from foundation to xtras. fastcore.net lost its request builders (urlrequest, urlsend, do_request, urlcheck) and clean_type_str is gone. parallel_gen is removed; the stdlib ProcessPoolExecutor initializer pattern replaces it (fastai’s parallel_tokenize shows the recipe). Python 3.11 or later is now required. If you need the old APIs, pin fastcore<2.

Python is a powerful, dynamic language. Rather than bake everything into the language, it lets the programmer customize it to make it work for them. fastcore uses this flexibility to add to Python features inspired by other languages we’ve loved, mixins from Ruby, and currying, binding, and more from Haskell. It also adds some “missing features” and cleans up some rough edges in the Python standard library, such as simplifying parallel processing, and bringing ideas from NumPy over to Python’s list type.

Here are some tips on using fastcore:

  • Liberal imports: Use from fastcore.module import * freely. The library is designed for safe wildcard imports.
  • Enhanced list operations: Substitute list with L. This provides advanced indexing, method chaining, and additional functionality while maintaining list-like behavior.
  • Extend existing classes: Apply the @patch decorator to add methods to classes, including built-ins, without subclassing.
  • Streamline class initialization: In __init__ methods, use store_attr() to efficiently set multiple attributes, reducing repetitive assignment code.
  • Explicit keyword arguments: Apply the delegates decorator to functions to replace **kwargs with specific parameters, enhancing IDE support and documentation.
  • Optimize parallel execution: Use fastcore’s enhanced ThreadPoolExecutor and ProcessPoolExecutor for simplified concurrent processing.
  • Expressive testing: Prefer fastcore’s testing functions like test_eq, test_ne, test_close for more readable and informative test assertions.
  • Advanced file operations: Use the extended Path class, which adds methods like ls(), read_json(), and others to pathlib.Path.
  • Flexible data structures: Convert between dictionaries and attribute-access objects using dict2obj and obj2dict for more intuitive data handling.
  • Functional programming paradigms: Use tools like compose, maps, and filter_ex to write more functional-style Python code.
  • Documentation: Use docments where possible to document parameters of functions and methods.
  • Time-aware caching: Apply the timed_cache decorator to add time-based expiration to the standard lru_cache functionality.
  • Simplified CLI creation: Use fastcore.script to easily transform Python functions into command-line interfaces.

For example, L is a drop-in replacement for list with extra superpowers:

x = L(1,2,3,4)
test_eq(x[[0,3]], [1,4])               # index with a collection
test_eq(x.map(lambda o:o*2), [2,4,6,8])
test_eq(x.filter(lambda o:o>2), [3,4])
x += [5]
test_eq(x.unique(), [1,2,3,4,5])

Tutorials

Getting started

To install fastcore run: conda install fastcore -c fastai (if you use Anaconda, which we recommend) or pip install fastcore. For an editable install, clone this repo and run: pip install -e ".[dev]". fastcore is tested to work on Ubuntu, macOS and Windows (versions tested are those shown with the -latest suffix here).

fastcore contains many features, including:

  • fastcore.test: Simple testing functions
  • fastcore.foundation: Mixins, delegation, composition, and more
  • fastcore.xtras: Utility functions to help with functional-style programming, parallel processing, and more

To get started, we recommend you read through the fastcore tour.

Contributing

After you clone this repository, please run nbdev_install_hooks in your terminal. This sets up git hooks, which clean up the notebooks to remove the extraneous stuff stored in the notebooks (e.g. which cells you ran) which causes unnecessary merge conflicts.

To run the tests in parallel, launch nbdev_test.

Before submitting a PR, check that the local library and notebooks match.

  • If you made a change to the notebooks in one of the exported cells, you can export it to the library with nbdev_prepare.
  • If you made a change to the library, you can export it back to the notebooks with nbdev_update.