How To Use: "@dataclass" In Python (Tutorial 2023)

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ต.ค. 2024
  • Here's a quick guide on how you can use dataclasses in Python, and how they help us avoid a lot of boilerplate code.
    ▶ Become job-ready with Python:
    www.indently.io
    ▶ Follow me on Instagram:
    / indentlyreels

ความคิดเห็น • 29

  • @velicihanates3288
    @velicihanates3288 ปีที่แล้ว +15

    You could have used two asterix operators to insert json in your person class
    'bob = Person(**json)'

    • @Indently
      @Indently  ปีที่แล้ว +6

      Only if your dataclass fits PERFECTLY the data from the JSON, otherwise it will throw an error.

    • @velicihanates3288
      @velicihanates3288 ปีที่แล้ว

      @@Indently thanks for telling, I really enjoy your python tutorials!

    • @thepackbot
      @thepackbot 11 หลายเดือนก่อน

      that's called unpacking.

  • @thepackbot
    @thepackbot 11 หลายเดือนก่อน +6

    That tidbit about "variables with default values must come after the ones without them", just explained a big problem I was having. Thank you!

  • @nargileh1
    @nargileh1 2 หลายเดือนก่อน

    bob = Person(json['name'],json['age'],json['job'],json['friends'])
    why not use this instead?
    bob = Person(**json)

  • @smanzoli
    @smanzoli 9 วันที่ผ่านมา

    It's important to talk about __post_init__ because real code WILL need it, dataclass cannot do all the __init__ most times.

  • @FrocketGaming
    @FrocketGaming 4 หลายเดือนก่อน +1

    Thanks for this. As I was watching and working on a selenium web scraping script I realized I could make a config dataclass to store all the css_selectors / xpaths I need for this script!

  • @davidl3383
    @davidl3383 ปีที่แล้ว +4

    Thank you. Very clear like your udemy couse ;)

  • @lostsoul8634
    @lostsoul8634 ปีที่แล้ว +5

    I'm learning python... bob= Person(**json) is better?

  • @aryandatla5188
    @aryandatla5188 หลายเดือนก่อน

    can we implement linked lists using dataclass?

  • @developer_anonymous
    @developer_anonymous ปีที่แล้ว +7

    'banana.calories = 69, i mean, 60' 😂

  • @kamurashev
    @kamurashev ปีที่แล้ว +3

    Nice! Named tuple vs dataclass for the next video?

    • @Indently
      @Indently  ปีที่แล้ว +2

      Possibly someday!

  • @jeyosman1
    @jeyosman1 6 หลายเดือนก่อน

    I just wanted to take a moment to express my sincere appreciation for your exceptional explanations and the engaging speed at which you communicate. Your clarity and enthusiasm truly stand out, making learning from you an absolute pleasure. Thank you for your dedication and talent-I genuinely admire the way you convey information

  • @adhd_arti
    @adhd_arti ปีที่แล้ว

    Cool. Thanks!

  • @NoidoDev
    @NoidoDev 11 หลายเดือนก่อน

    Is it better to use that for data or some dict? For example, It seem that I won't be able to get "keywords".
    Also, there's a difference between __slots__ and slots=True, which is that you can use default value with the new method.

  • @pankuang-q1k
    @pankuang-q1k 6 หลายเดือนก่อน

    your explaination is very helpful for understanding the question 'why we need to do that ', and i like your explaination for reading a json file to an object. Thank you very much for this good video.

  • @chromery
    @chromery 10 หลายเดือนก่อน

    Great video as always! Have you by any chance made a video about Pydantic (couldn't find it), or are you maybe planning to?

  • @enricoroselino7557
    @enricoroselino7557 11 หลายเดือนก่อน +1

    bob job nice

  • @kostasmessinis
    @kostasmessinis 7 หลายเดือนก่อน +1

    good

  • @forethiocodingstaff
    @forethiocodingstaff ปีที่แล้ว

    Young, energetic python developer

  • @mecrayavcin
    @mecrayavcin 2 หลายเดือนก่อน

    Nice video

  • @Timmie_Tudor
    @Timmie_Tudor ปีที่แล้ว

    Why are you teaching people how to use me?

    • @Indently
      @Indently  ปีที่แล้ว

      Because you're great and deserve to be treated with respect.

  • @SanwicklithMkalinga-g6h
    @SanwicklithMkalinga-g6h ปีที่แล้ว

    Do u have a course… or mentoring program

    • @Indently
      @Indently  ปีที่แล้ว

      I have a couple of courses at: indently.io

  • @DrDeuteron
    @DrDeuteron ปีที่แล้ว

    Don't forget the dataclasses.Field object...can be very useful when dealing with telemetry.
    There's also the "astuple" method that is attached to the class.

  • @betapacket
    @betapacket ปีที่แล้ว

    5th