Mastering Python Refactoring: Simplifying Code with the Dictionary Dispatch Pattern

Поделиться
HTML-код
  • Опубликовано: 19 ноя 2024

Комментарии • 15

  • @sckhoo
    @sckhoo Год назад +1

    the beauty of python is that, it is a new concept to me, but if i read the code, i know what it does.

  • @Pulenbach
    @Pulenbach Год назад +2

    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

    • @Pybites
      @Pybites  Год назад

      Thanks! What other refactorings would you like to see?

    • @Pulenbach
      @Pulenbach Год назад +1

      Something similar related to data structures, tricks and skills which we can use everyday in practice during defining methods, classes etc. Thanks! @@Pybites

    • @Pybites
      @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?

  • @michalbotor
    @michalbotor Год назад +1

    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()
    ```

    • @Pybites
      @Pybites  Год назад

      Nice, thanks for sharing 🐍 😍

  • @illiakhoroshykh6016
    @illiakhoroshykh6016 Год назад +1

    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?

    • @Pybites
      @Pybites  Год назад +1

      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) :)

  • @mirat3227
    @mirat3227 Год назад +1

    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

    • @Pybites
      @Pybites  Год назад

      Thanks! Interesting, do you have a more complete code example, e.g. a gist?

  • @ciaran9615
    @ciaran9615 Год назад +1

    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.

    • @Pybites
      @Pybites  Год назад

      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 ...

  • @matteodelgrosso4364
    @matteodelgrosso4364 Год назад

    Even better: just access the dict and catch the KeyError exception.