Understanding Python: Decorators

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

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

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

    A very clear presentation of a topic that I don’t find particularly intuitive.
    Thanks!!
    I want to play around more now to see what kinds of changes decorators can make.

  • @dragonkat13
    @dragonkat13 4 года назад +3

    I'm happy to see that you took my advice and did the one on decorators! well done! :)

  • @d-shiri
    @d-shiri 2 года назад +1

    great explanation

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

    The last example (return_type decorator) seems like a nice way to strictly enforce types in function arguments without too much of hassle. Type hints are great (and amazing with respect to readability), but it does not ensure runtime validation of types, while this seems to do the trick just fine. Wasn't expecting to walk out of this video with this. Thanks!

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

      That's one of the reasons I'm not a fan of python's type system, because it largely is just a hint without additional tooling.
      If you find any other awesome decorator uses, come back and let us know!

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

    Why couldn't add_attributes take the func by itself? Why does it need the decorator function?
    Before this video, I didn't even know decorators were a thing in Python.

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

      Great question! An important thing to keep in mind is that the actual decorator only ever takes one argument, and that is the function it is decorating. So in order for us to build in this customized behavior we write the add_attributes function. This is what creates and returns the actual decorator at runtime.

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

      @@JakeCallahan That makes sense, thanks