the video is amazing, this kind of "refactor" video would be much helpful for python devs who wants to improve their coding skills :) very practical, thanks
Something similar related to data structures, tricks and skills which we can use everyday in practice during defining methods, classes etc. Thanks! @@Pybites
@@Pulenbach I (Bob) listed some refactoring video ideas here: www.linkedin.com/posts/bbelderbos_python-refactoring-cleancode-activity-7105133600022351872-RaMF? (hope you can see that) What specifically around data structures would you like to see?
in python functions are first class citizens you can also do this ``` from enum import Enum from typing import Callable, Union Num = Union[int, float] Fn = Callable[[Num], Num] class Op(Enum): def add(a: Num, b: Num) -> Num: return a + b def sub(a: Num, b: Num) -> Num: return a - b def perform(op: Fn, a: Num, b: Num) -> Num: return op(a, b) def main(): a = 1 b = 2 print(perform(Op.add, a, b)) # 3 print(perform(Op.sub, a, b)) # -1 print(perform(Op.mul, a, b)) # !crash - not sure if this should be handled or not main() ```
Great video, thanks! One of my favorite tricks in all Python programming. Wanted to ask: did you consider structural pattern matching as full replacement for this technique? Maybe you see some limitations in it?
I did not for this case, as commented elsewhere here I would use that for more complex patterns, although I have not seriously used that feature yet in production code so no expert (yet) :)
Thanks a lot for the video! Actually my favorite pattern with Python, thanks to the fact that functions are first class objects. What do you think about the idea of providing the respective operation functions with a self-registering decorator and thus automatically adding them to the OPERATIONS-dictionary. I like the idea, or maybe not explicit enough? def auto_register(func): """Automatically store an operation function in operations dict""" OPERATIONS[func.__name__] = func return func
Is there an efficiency gain between what is shown in the video and using the more recent Python Structural Pattern matching to do the dispatching? PEP 636 for reference.
The two are different: If the goal is purely dispatch based on keys (as per the scenario in this video), a dictionary is a compelling choice (also, O(1) dict lookups are genuinely fast). However, if you're handling more complex scenarios with varied data structures and conditions, then Structural Pattern Matching becomes a powerful tool. So it depends the scenario at hand ...
the beauty of python is that, it is a new concept to me, but if i read the code, i know what it does.
🐍 😍
the video is amazing, this kind of "refactor" video would be much helpful for python devs who wants to improve their coding skills :) very practical, thanks
Thanks! What other refactorings would you like to see?
Something similar related to data structures, tricks and skills which we can use everyday in practice during defining methods, classes etc. Thanks! @@Pybites
@@Pulenbach I (Bob) listed some refactoring video ideas here: www.linkedin.com/posts/bbelderbos_python-refactoring-cleancode-activity-7105133600022351872-RaMF? (hope you can see that)
What specifically around data structures would you like to see?
in python functions are first class citizens
you can also do this
```
from enum import Enum
from typing import Callable, Union
Num = Union[int, float]
Fn = Callable[[Num], Num]
class Op(Enum):
def add(a: Num, b: Num) -> Num:
return a + b
def sub(a: Num, b: Num) -> Num:
return a - b
def perform(op: Fn, a: Num, b: Num) -> Num:
return op(a, b)
def main():
a = 1
b = 2
print(perform(Op.add, a, b)) # 3
print(perform(Op.sub, a, b)) # -1
print(perform(Op.mul, a, b)) # !crash - not sure if this should be handled or not
main()
```
Nice, thanks for sharing 🐍 😍
Great video, thanks! One of my favorite tricks in all Python programming.
Wanted to ask: did you consider structural pattern matching as full replacement for this technique? Maybe you see some limitations in it?
I did not for this case, as commented elsewhere here I would use that for more complex patterns, although I have not seriously used that feature yet in production code so no expert (yet) :)
Thanks a lot for the video! Actually my favorite pattern with Python, thanks to the fact that functions are first class objects. What do you think about the idea of providing the respective operation functions with a self-registering decorator and thus automatically adding them to the OPERATIONS-dictionary. I like the idea, or maybe not explicit enough?
def auto_register(func):
"""Automatically store an operation function in operations dict"""
OPERATIONS[func.__name__] = func
return func
Thanks! Interesting, do you have a more complete code example, e.g. a gist?
Is there an efficiency gain between what is shown in the video and using the more recent Python Structural Pattern matching to do the dispatching? PEP 636 for reference.
The two are different:
If the goal is purely dispatch based on keys (as per the scenario in this video), a dictionary is a compelling choice (also, O(1) dict lookups are genuinely fast).
However, if you're handling more complex scenarios with varied data structures and conditions, then Structural Pattern Matching becomes a powerful tool.
So it depends the scenario at hand ...
Even better: just access the dict and catch the KeyError exception.