3 ms·
Not knowing python, I find the data classes example extremely readable. More so than Ruby example.
by alberth 11mo ago
Not knowing python, I find the data classes example extremely readable. More so than Ruby example.
- disgruntledphd2 11mo agoI write mostly Python these days, but agree with op. The comparables implementation in Ruby seems much nicer to me (maybe because I'm less familiar with it).
- js2 11mo agoIt's virtually the same in Python if you wrote it explicitly: def <=>(other) [major, minor, patch] <=> [other.major, other.minor, other.patch] end vs: def __lt__(self, other): return (self.major, self.minor, self.patch) < (other.major, other.minor, other.patch) Then use the `total_ordering` decorator to provide the remaining rich comparison methods. That said, it's a little annoying Python didn't keep __cmp__ around since there's no direct replacement that's just as succinct and what I did above is a slight fib: you still may need to add __eq__() as well.
- zahlman 11mo ago> Then use the `total_ordering` decorator to provide the remaining rich comparison methods. While we're here, worth highlighting `cmp_to_key` as well for `sorted` etc. calls. > it's a little annoying Python didn't keep __cmp__ around since there's no direct replacement that's just as succinct The rationale offered at the time (https://docs.python.org/3/whatsnew/3.0.html https://docs.python.org/3/whatsnew/3.0.html) was admittedly weak, but at least this way there isn't confusion over what happens if you try to use both ways (because one of them just isn't a way any more).
- disgruntledphd2 11mo agoI know, but the ability to use symbols to define the comparator is super, super cool, as opposed to the horrendously ugly lt dunder method.
- jimbokun 11mo agoIt's a little magicky. I guess the "Order=True" is what ensures the order of the parameters in the auto-generated constructor matches the order in which the instance variables are defined?
- js2 11mo agoorder: If true (the default is False), __lt__(), __le__(), __gt__(), and __ge__() methods will be generated. These compare the class as if it were a tuple of its fields, in order. eq: If true (the default), an __eq__() method will be generated. This method compares the class as if it were a tuple of its fields, in order. Both instances in the comparison must be of the identical type.
- jimbokun 11mo agoHowever, I think comparing the Ruby example implementation with the "data classes example" is a category error. The Ruby example should be compared to the implementation of data classes. The Ruby code shows how cleanly the code for parsing, comparing and printing a version string can be. We would need to see the code underlying the data classes implementation to make a meaningful comparison.