Traitlets: Python Configuration System
Traitlets is a Python library that provides a framework for defining attributes with type checking, dynamic default values, and change callbacks. It is currently at version 5.14.3, released on April 19, 2024, and follows a regular release cadence with periodic updates and maintenance improvements.
Warnings
- breaking Traitlets 5.0 removed support for Python 2 and versions 3.0-3.6, now requiring Python 3.7 or higher.
- deprecated The 'six' library is no longer a dependency as of Traitlets 5.0.0.
- gotcha Using 'default' as a method name without the '@default' decorator will not set the default value for the trait.
Install
-
pip install traitlets
Imports
- HasTraits
from traitlets import HasTraits
- Unicode
from traitlets import Unicode
- Int
from traitlets import Int
- default
from traitlets import default
Quickstart
import getpass
from traitlets import HasTraits, Unicode, default
class Identity(HasTraits):
username = Unicode()
@default('username')
def _default_username(self):
return getpass.getuser()
identity = Identity()
print(identity.username)