Inheritence and Dunder Methods - Python Classes

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ต.ค. 2024

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

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

    For classes where you are essentially using them just to hold related data, you should look into the @dataclass decorator. This will write things like the __init__ , __repr__ and __eq__ for you automatically (the latter using piece-wise comparisons, read the documentation).
    ```
    from dataclasses import dataclass
    @dataclass
    class Person():
    name: str
    age: int
    @dataclass Developer(Person):
    salary: int
    ```
    This cuts out a lot of boilerplate code

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

      Great suggestion! Thanks Icarus!

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

    Starting to make sense but at this stage I can't see myself using classes yet, just a heads up mate, we always decrease age, never increase

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

      😂