A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.md 4.4KB

2 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. title: dataklasses: A different spin on dataclasses.
  2. url: https://github.com/dabeaz/dataklasses#questions-and-answers
  3. hash_url: 891705d1555d09a941fd1f7685de9370
  4. Dataklasses is a library that allows you to quickly define data
  5. classes using Python type hints. Here's an example of how you use it:
  6. ```python
  7. from dataklasses import dataklass
  8. @dataklass
  9. class Coordinates:
  10. x: int
  11. y: int
  12. ```
  13. The resulting class works in a well civilised way, providing the usual
  14. `__init__()`, `__repr__()`, and `__eq__()` methods that you'd normally
  15. have to type out by hand:
  16. ```python
  17. >>> a = Coordinates(2, 3)
  18. >>> a
  19. Coordinates(2, 3)
  20. >>> a.x
  21. 2
  22. >>> a.y
  23. 3
  24. >>> b = Coordinates(2, 3)
  25. >>> a == b
  26. True
  27. >>>
  28. ```
  29. It's easy! Almost too easy.
  30. ## Wait, doesn't this already exist?
  31. No, it doesn't. Yes, certain naysayers will be quick to point out the
  32. existence of `@dataclass` from the standard library. Ok, sure, THAT
  33. exists. However, it's slow and complicated. Dataklasses are neither
  34. of those things. The entire `dataklasses` module is less than 100
  35. lines. The resulting classes import 15-20 times faster than
  36. dataclasses. See the `perf.py` file for a benchmark.
  37. ## Theory of Operation
  38. While out walking with his puppy, Dave had a certain insight about the nature
  39. of Python byte-code. Coming back to the house, he had to try it out:
  40. ```python
  41. >>> def __init1__(self, x, y):
  42. ... self.x = x
  43. ... self.y = y
  44. ...
  45. >>> def __init2__(self, foo, bar):
  46. ... self.foo = foo
  47. ... self.bar = bar
  48. ...
  49. >>> __init1__.__code__.co_code == __init2__.__code__.co_code
  50. True
  51. >>>
  52. ```
  53. How intriguing! The underlying byte-code is exactly the same even
  54. though the functions are using different argument and attribute names.
  55. Aha! Now, we're onto something interesting.
  56. The `dataclasses` module in the standard library works by collecting
  57. type hints, generating code strings, and executing them using the
  58. `exec()` function. This happens for every single class definition
  59. where it's used. If it sounds slow, that's because it is. In fact, it
  60. defeats any benefit of module caching in Python's import system.
  61. Dataklasses are different. They start out in the same manner--code is
  62. first generated by collecting type hints and using `exec()`. However,
  63. the underlying byte-code is cached and reused in subsequent class
  64. definitions whenever possible. Caching is good.
  65. ## A Short Story
  66. Once upon a time, there was this programming language that I'll refer
  67. to as "Lava." Anyways, anytime you started a program written in Lava,
  68. you could just tell by the awkward silence and inactivity of your
  69. machine before the fans kicked in. "Ah shit, this is written in Lava"
  70. you'd exclaim.
  71. ## Questions and Answers
  72. **Q: What methods does `dataklass` generate?**
  73. A: By default `__init__()`, `__repr__()`, and `__eq__()` methods are generated.
  74. `__match_args__` is also defined to assist with pattern matching.
  75. **Q: Does `dataklass` enforce the specified types?**
  76. A: No. The types are merely clues about what the value might be and
  77. the Python language does not provide any enforcement on its own.
  78. **Q: Are there any additional features?**
  79. A: No. You can either have features or you can have performance. Pick one.
  80. **Q: Does `dataklass` use any advanced magic such as metaclasses?**
  81. A: No.
  82. **Q: How do I install `dataklasses`?**
  83. A: There is no `setup.py` file, installer, or an official release. You
  84. install it by copying the code into your own project. `dataklasses.py` is
  85. small. You are encouraged to modify it to your own purposes.
  86. **Q: What versions of Python does it work with?**
  87. A: The code will work with versions 3.9 and later.
  88. **Q: But what if new features get added?**
  89. A: What new features? The best new features are no new features.
  90. **Q: Who maintains dataklasses?**
  91. A: If you're using it, you do. You maintain dataklasses.
  92. **Q: Is this actually a serious project?**
  93. A: It's best to think of dataklasses as more of an idea than a project.
  94. There are many tools and libraries that perform some form of code generation.
  95. Dataklasses is a proof-of-concept for a different approach to that. If you're
  96. a minimalist like me, you'll find it to be perfectly functional. If you're
  97. looking for a lot of knobs to turn, you should probably move along.
  98. **Q: Should I give dataklasses a GitHub star?**
  99. A: Yes, because it will help me look superior to the other parents with
  100. kids in the middle-school robot club.
  101. **Q: Who wrote this?**
  102. A: `dataklasses` is the work of David Beazley. http://www.dabeaz.com.