Comments (2)
Hi @ajing, thank you for your question!
First, in general I think it's a good idea for objects to be immutable when possible, because it makes them easier to reason about, harder to make mistakes with, and easier to reuse in large programs. In this particular case it was a harder decision, because a mutable API would be much simpler: we wouldn't have needed a separate FlowBuilder object at all. Still, there were enough concrete benefits to immutability that we thought it was worth it. I'll give two examples.
- Suppose you're using a Flow in a notebook like this:
flow.get('result_plot')
flow.setting('preprocessing_type', 'pca').get('result_plot')
flow.setting('ensemble_size', 200).get('result_plot')
If flow
were mutable, then each of these cells would be modifying a shared object and their outputs would depend on what order you ran them in. This would be really confusing and annoying! The immutable API lets you keep them separate.
- Suppose you want to use a flow as part of another library, like this:
from .prediction_pipeline import prediction_flow
def predict(features, priors=None):
flow = prediction_flow.setting('features', features)
if priors is not None:
flow = flow.setting('priors', priors)
return flow.get('prediction')
Like before, if the flow object were mutable, then each call to predict
might affect the priors
value for subsequent calls.
Of course, we could offer both types of API in the same class, or we could allow users to just make a copy of the flow whenever they want to avoid global changes. However, this makes things even more complicated and makes it easier to make mistakes. The Flow/FlowBuilder approach lets you guarantee that no one will modify your flow object, but still lets you switch to a mutable API when necessary. This seemed like a good compromise.
Finally, as you noted, it's also true that allowing mutation would make things like reloading and parallelism more complicated. These weren't the main motivations, but you get a lot of little benefits like this automatically when you make things immutable.
I hope this answered your question! If you have any more questions or feedback, I'd be happy to either talk here or email you directly.
from bionic.
Thanks for the clarification! This is very useful.
from bionic.
Related Issues (20)
- Errors from unset entities are hard to debug
- @changes_per_run generates extra cache entries
- Non-persistable entities can sometimes be spuriously recomputed
- Redefining an entity with a function can produce surprising results
- The "provider" concept is too broad
- Should bionic.util.init_basic_logging only affect logging for bionic components?
- Concerning exception messages in pytest on Python 3.8 HOT 4
- Compute task states in the subprocesses
- Multiple processes and threads? HOT 1
- Full test suite fails on OS X Catalina HOT 1
- Caching fails when directory names contain spaces
- `FlowBuilder.merge` not accepting another builder is confusing
- Reloading of flow spanning multiple files HOT 1
- Docs: Functions with multiple outputs cannot be called explicitly HOT 2
- Uncached values should be recomputed each time they're used HOT 1
- Set-valued entities are not hashed determistically
- Confusing error message when non-iterable input used with `outputs`
- parallel evaluation inconsistencies with persist settings
- Improve error message when protocols fail
- Test vector update script should delete old test vectors
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bionic.