Does Pydantic Replace Dataclasses in Python?

Поділитися
Вставка
  • Опубліковано 19 чер 2024
  • Today we learn about Pydantic and discuss whether it replaces Python's dataclasses or not.
    ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
    📚 Programming Books & Merch 📚
    🐍 The Python Bible Book: www.neuralnine.com/books/
    💻 The Algorithm Bible Book: www.neuralnine.com/books/
    👕 Programming Merch: www.neuralnine.com/shop
    🌐 Social Media & Contact 🌐
    📱 Website: www.neuralnine.com/
    📷 Instagram: / neuralnine
    🐦 Twitter: / neuralnine
    🤵 LinkedIn: / neuralnine
    📁 GitHub: github.com/NeuralNine
    🎙 Discord: / discord
    🎵 Outro Music From: www.bensound.com/
    Timestamps:
    (0:00) Intro
    (0:20) Pydantic Fundamentals
    (3:16) Validating Fields
    (9:43) Root Validators
    (12:55) Parsing JSON Data
    (16:38) Outro
  • Наука та технологія

КОМЕНТАРІ • 28

  • @dariuszspiewak5624
    @dariuszspiewak5624 Рік тому +10

    The nested if's can easily be turned into a sequence of guard clauses, which should ease the burden on the eyes and boost the understanding of what the code is supposed to do. Just a hint... :)

  • @w2lkm2n
    @w2lkm2n 2 роки тому +15

    No, it doesn't! They are completely different things. Pydantic is a validation and serialization library, dataclasses is essentially a code generation tool for classes, which help you avoid boilerplate when defining classes. They have totally different features, and are completely different things!
    If you would use pydantic instead of dataclasses, your code would be way slower. You can't use dataclasses instead of pydantic, because it doesn't provide serialization and you can't really validate user input with it.

    • @horoshuhin
      @horoshuhin 2 роки тому

      That makes sense. not everything needs validation. If you work with the data you most certainly should consider pydantic. on that note. is the code slower because it has to do serialization?

  • @zakariaabderrahmanesadelao3048

    Amazing and clear explanation as usual! However, one criticism I have is regarding the speed of delivery of information. Nonetheless, we appreciate your abundant knowledge

  • @kosmonautofficial296
    @kosmonautofficial296 Рік тому

    thanks this helped me out I really need to start using this!

  • @eugenung94
    @eugenung94 2 роки тому +3

    Great explanations! Thanks! Your videos are very useful

  • @meisj
    @meisj 11 місяців тому +1

    Hey can we put constructor inside? Tried it but when I do the validators are not invoked.

  • @jaypatel3233
    @jaypatel3233 2 роки тому +2

    can we have more advance video on this ? bdw Kudos for this !!😉

  • @cyberpunkalphamale
    @cyberpunkalphamale Рік тому

    Great breakdown

  • @obed818
    @obed818 2 роки тому +1

    Thanks you very much 👍

  • @gnani-re3sj
    @gnani-re3sj 6 місяців тому

    i have a doubt, how to destructor the JSON file, if we have nested objects inside an array ?

  • @nithinjp1
    @nithinjp1 11 місяців тому

    So are you the guy that writes the chatgpt produced codes , because you are amazing. Dammn

  • @aguspe532
    @aguspe532 2 роки тому

    Great explanations! Thanks!

  • @pucek365
    @pucek365 2 роки тому +1

    Hello, can someone explain to me, why there is a @classmethod before defining a validator? I think it's optional, because the documentation on pydantic does not mention that. I'm just curious why it is there

    • @NeuralNine
      @NeuralNine  2 роки тому +3

      It is a decorator from Core-Python to indicate that a method belongs to the class and not to a specific instance. We also have the cls parameter instead of the self parameter.

    • @pucek365
      @pucek365 2 роки тому

      @@NeuralNine Thanks for the answer :)

    • @__magicman__
      @__magicman__ Рік тому

      @@NeuralNine Isn't it the case that Python 3.11 is going to remove chaining decorators? I checked the pydantic documentation just now and @pucek365 seems to be right. There is no classmethod decorator but they use cls if they decorate with validator. They probably changed it because of forward compatibility.

  • @trez6465
    @trez6465 Рік тому

    could you post the git repo?

  • @user-wd6nq3wr4d
    @user-wd6nq3wr4d 9 місяців тому

    excelente explicación

  • @DanielRodriguez-el1gb
    @DanielRodriguez-el1gb Рік тому

    What could we do if someone makes an assigment to an attribute after the instance? As far as i understand, custom validations are not executed when that happens.

    • @aflous
      @aflous Рік тому

      well, that's the role of a property setter. You can also patch the __setattr__ of your base model to achieve that

  • @irmdev595
    @irmdev595 2 роки тому

    for root_validators
    the "values" is a pydantic keyword? how does it know that "email" is one of the fields?
    more dataclasses with functions inside the dataclass!

  • @amitwali6844
    @amitwali6844 Рік тому

    Any way to sort the data?

  • @tcgvsocg1458
    @tcgvsocg1458 2 роки тому

    really good

  • @stevenwilson5556
    @stevenwilson5556 Рік тому

    Not sure what I am doing wrong but the code does not work for me. Using Python 3.10.7 64-bit

  • @philtoa334
    @philtoa334 2 роки тому

    Thx.

  • @erikchesteranofrethcanavir2365
    @erikchesteranofrethcanavir2365 2 роки тому

    CONGRATULATIONS

  • @szymonzywko9315
    @szymonzywko9315 Рік тому

    I cried when i saw this nasted if's in 7:30