Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/source/best-practices.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Best Practices

A collection of useful design tips & best practices for using traitlets in your
application or library.

## Avoid using "pure config" objects

Put your traitlets config directly on the classes that are doing the work,
rather than creating separate classes just for holding configuration. This
keeps configuration close to the code it is configuring, and helps with
discoverability as well.

So instead of:

```python
class SpawnerConfig(LoggingConfigurable):
some_config = Unicode("", config=True)

some_other_config = Unicode("default", config=True)


class Spawner:
def spawn(self, config: SpawnerConfig):
print(config.some_config)
# Do things with config
```

Prefer:

```python
class Spawner(LoggingConfigurable):
some_config = Unicode("", config=True)

some_other_config = Unicode("default", config=True)

def spawn(self):
print(self.some_config)
```
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ the configuration machinery.
trait_types
defining_traits
api
best-practices
config
config-api
utils
Expand Down