|
|
|
|
|
by abuckenheimer
2904 days ago
|
|
I don't know the Kirill Balunov example is pretty beautiful/simple/flat/readable if reductor := dispatch_table.get(cls):
rv = reductor(x)
elif reductor := getattr(x, "__reduce_ex__", None):
rv = reductor(4)
elif reductor := getattr(x, "__reduce__", None):
rv = reductor()
else:
raise Error("un(shallow)copyable object of type %s" % cls)
especially when you compare it to the existing implementation: reductor = dispatch_table.get(cls)
if reductor:
rv = reductor(x)
else:
reductor = getattr(x, "__reduce_ex__", None)
if reductor:
rv = reductor(4)
else:
reductor = getattr(x, "__reduce__", None)
if reductor:
rv = reductor()
else:
raise Error("un(shallow)copyable object of type %s" % cls)
|
|
Once you abstract the patterns you can drive towards data-driven code:
Syntax is the enemy, never substitute syntax for thought.