You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I often, when writing demos, find myself doing something like this:
@attrs
class MyController:
controller_param_1 = attrib(default=0.4)
controller_param_2 = attrib(default=7)
def update(self, x):
...
return motor
def run_demo(dataset_name, n_samples = 100, controller_param_1=0.4, controller_param_2=7):
controller = MyController(controller_param_1=controller_param_1, controller_param_2=controller_param_2)
for x in get_data(dataset_name, n_samples):
print(controller.update(x))
Yes, I know I could just pass in MyController to run_demo(...) instead of the params - and that (dependency injection) is the right way to do things when designing scalable library code, but it's often easier to just pass in the args directly - run_demo(...) is easier to understand when you don't have to think about how to construct the arguments.
The trouble is that now we've defined defaults controller_param_1=0.4, controller_param_2=7 in multiple places, which violates "Don't Repeat Yourself" and can very easily lead to bugs.
Yes, I know I could just replace controller_param_1=0.4, controller_param_2=7 with **params, but that just obfuscates the interface of run_demo(...) which is exactly what we were trying to avoid in the first place.
My proposal is to just have an attr.DEFERRED object which, when passed as a constructor argument of an attr object, just looks up the default. i.e. change run_demo to:
def run_demo(dataset_name, n_samples = 100, controller_param_1=attr.DEFERRED, controller_param_2=attr.DEFERRED):
controller = MyController(controller_param_1=controller_param_1, controller_param_2=controller_param_2)
for x in get_data(dataset_name, n_samples):
print(controller.update(x))
The text was updated successfully, but these errors were encountered:
I often, when writing demos, find myself doing something like this:
Yes, I know I could just pass in
MyController
torun_demo(...)
instead of the params - and that (dependency injection) is the right way to do things when designing scalable library code, but it's often easier to just pass in the args directly -run_demo(...)
is easier to understand when you don't have to think about how to construct the arguments.The trouble is that now we've defined defaults
controller_param_1=0.4, controller_param_2=7
in multiple places, which violates "Don't Repeat Yourself" and can very easily lead to bugs.Yes, I know I could just replace
controller_param_1=0.4, controller_param_2=7
with**params
, but that just obfuscates the interface ofrun_demo(...)
which is exactly what we were trying to avoid in the first place.My proposal is to just have an
attr.DEFERRED
object which, when passed as a constructor argument of an attr object, just looks up the default. i.e. change run_demo to:The text was updated successfully, but these errors were encountered: