Earlier quoted context omitted.
It's not. See [1]. The retrofitting of type declarations to Python is taking a very strange path. (Evil interpretation: this is an effort by Python's little tin god to sabotage PyPy. Type information is a huge win for a compiler; it can now generate hard code for a specific type. It doesn't help CPython much, since inside CPython everything is a CObject. With type information, one could write things like NumPy, with…
How is this "sabotaging" PyPy? Also, even with type declarations, I'm not sure you could get Numpy-esque performance, given that Numpy doesn't have to dereference every element in the array.
Using attrs for everything in Python
51–60 of 105 posts
Re: Using attrs for everything in Python
#52Earlier quoted context omitted.
How is this "sabotaging" PyPy? Also, even with type declarations, I'm not sure you could get Numpy-esque performance, given that Numpy doesn't have to dereference every element in the array.
If the compiler knows a Python array is uniformly of type float, it can, and should, use a dense array of floats to represent it.
Re: Using attrs for everything in Python
#53While the conciseness of attrs is nice, it's not very readable. This: class Point3D(object): def __init__(self, x, y, z): self.x = x self.y = y self.z = z ...is much easier to read than this: import attr @attr.s class Point3D(object): x = attr.ib() y = attr.ib() z = attr.ib() Readability is what I love about Python. I don't think the conciseness of attrs is worth the loss in readability.
Aside from that, this is not a fair comparison because attr names can, and mostly should be, longer, and there can be more of them, e.g.:
class SomeClass(object):
def __init__(self, myattr1, some_val, a_bool,
my_other_attr):
self.myattr1 = myattr1
self.some_val = some_val
self.a_bool = a_bool
self.my_other_attr = my_other_attr
And of course they'll need to go into __str__ method as well.Re: Using attrs for everything in Python
#54While the conciseness of attrs is nice, it's not very readable. This: class Point3D(object): def __init__(self, x, y, z): self.x = x self.y = y self.z = z ...is much easier to read than this: import attr @attr.s class Point3D(object): x = attr.ib() y = attr.ib() z = attr.ib() Readability is what I love about Python. I don't think the conciseness of attrs is worth the loss in readability.
As I think others noted, the attr version does alot more: it adds representation and easily adds compare methods as shown in OP. Aside from that, this is not a fair comparison because attr names can, and mostly should be, longer, and there can be more of them, e.g.: class SomeClass(object): def __init__(self, myattr1, some_val, a_bool, my_other_attr): self.myattr1 = myattr1 self.some_val = some_val self.a_bool = a_bo…
"Explicit is better than implicit"
Re: Using attrs for everything in Python
#55Re: Using attrs for everything in Python
#56We use Lombok a fair amount within our code to abstract away a lot of the class setup, which has been nice. But as I move more towards the Python world, I'm interested to see how attr fits in.
Re: Using attrs for everything in Python
#57Stuff like @attr.ib is kind of cute looking. (This not being a good thing).
Though in reality, I'd probably want more explicit methods. If you are using something like Django REST framework, you can validate with the serializer. And sometimes you want to validate (often) the combination of variables and how they interact.
Kind of feels a little non-pythonic to me. Clever, but the ways it is changing things are stuff you have to unroll if the behavior grows, versus stuff you just add to.
Re: Using attrs for everything in Python
#58Earlier quoted context omitted.
As I think others noted, the attr version does alot more: it adds representation and easily adds compare methods as shown in OP. Aside from that, this is not a fair comparison because attr names can, and mostly should be, longer, and there can be more of them, e.g.: class SomeClass(object): def __init__(self, myattr1, some_val, a_bool, my_other_attr): self.myattr1 = myattr1 self.some_val = some_val self.a_bool = a_bo…
It adds magic. "Explicit is better than implicit" ~ https://en.wikipedia.org/wiki/Zen_of_Python
Re: Using attrs for everything in Python
#59Hmm, IDK. Stuff like @attr.ib is kind of cute looking. (This not being a good thing). Though in reality, I'd probably want more explicit methods. If you are using something like Django REST framework, you can validate with the serializer. And sometimes you want to validate (often) the combination of variables and how they interact. Kind of feels a little non-pythonic to me. Clever, but the ways it is changing things…
(If you don’t like the playful attr.s and attr.ib, you can also use their no-nonsense aliases attr.attributes and attr.attr).