Using attrs for everything in Python
71–80 of 105 posts
Re: Using attrs for everything in Python
#72Earlier 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
#73"Another place you probably should be defining an object is when you have a bag of related data that needs its relationships, invariants, and behavior explained. Python makes it soooo easy to just define a tuple or a list." Yes, and defining a tuple or a list is often better than a small object because of Python's problems with serializing (pickling) objects. If you're doing anything with data, anything functional, a…
None of this is a problem with attrs. Serialize however you like. >>> import attr >>> >>> @attr.s ... class Thing(object): ... a = attr.ib() ... b = attr.ib() ... ... >>> @attr.s ... class Many(object): ... things = attr.ib() ... ... >>> many = Many([Thing(1, 2), Thing(3, 4)]) >>> many Many(things=[Thing(a=1, b=2), Thing(a=3, b=4)]) >>> attr.asdict(many) {'things': [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]} >>> import pic…
You call attr.asdict() and it searches the attribute values, including inside lists, for more attr objects to convert into dict values?
What kinds of values does it search through? The documentation doesn't say, it just gives an example where it works inside a list for some reason.
Re: Using attrs for everything in Python
#74I can kind of understand something like this in Ruby, as it kind of encourages this (and it also has nicer class string-ification by default), but in Python I'd rather have all of my logic be explicitly stated out at the expense of less boilerplate. Also how often do you really need to overwrite those gt/lt methods in production code? If I saw that, rather than an explicit e.g. `.gt` method, I'd consider that a code smell.
Re: Using attrs for everything in Python
#75Earlier quoted context omitted.
Agreed. From the post, it seems to me like another (IMO better) alternative to this awkward attr-specific convention and syntax etc would be to create a factory function for dynamic class definition, in a similar vein to namedtuples. This is functionally similar to the metaclass route, and to be honest I'm not sure what the tradeoffs would be. Either way, I think metaprogramming is a much better approach than attrs i…
There is one: >>> C2 = attr.make_class("C2", ["a", "b"]) >>> C2("foo", "bar") C2(a='foo', b='bar')
Re: Using attrs for everything in Python
#76Earlier quoted context omitted.
It adds magic. "Explicit is better than implicit" ~ https://en.wikipedia.org/wiki/Zen_of_Python
I feel like a person should be able to back their opinion up (for a specific case, not in general), instead of just mindlessly quoting a line from a bible.
Re: Using attrs for everything in Python
#77Re: Using attrs for everything in Python
#78"Another place you probably should be defining an object is when you have a bag of related data that needs its relationships, invariants, and behavior explained. Python makes it soooo easy to just define a tuple or a list." Yes, and defining a tuple or a list is often better than a small object because of Python's problems with serializing (pickling) objects. If you're doing anything with data, anything functional, a…
Re: Using attrs for everything in Python
#79Earlier quoted context omitted.
None of this is a problem with attrs. Serialize however you like. >>> import attr >>> >>> @attr.s ... class Thing(object): ... a = attr.ib() ... b = attr.ib() ... ... >>> @attr.s ... class Many(object): ... things = attr.ib() ... ... >>> many = Many([Thing(1, 2), Thing(3, 4)]) >>> many Many(things=[Thing(a=1, b=2), Thing(a=3, b=4)]) >>> attr.asdict(many) {'things': [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]} >>> import pic…
...how does it do that? You call attr.asdict() and it searches the attribute values, including inside lists, for more attr objects to convert into dict values? What kinds of values does it search through? The documentation doesn't say, it just gives an example where it works inside a list for some reason.
Re: Using attrs for everything in Python
#80Earlier quoted context omitted.
Assigning a variable to a destination declared to be an incompatible type should produce an error at compile time if possible, and at at run time otherwise. That's what type declarations are all about.
I think you misunderstand. Here's an example: ``` foos = [Foo(a=0), Foo(a=1)] f0 = foos[0] foos[0] = Foo(a=2) print(f0.a) # 0 print(foos[0].a) # 2 ``` `foos` is a list of type `Foo`, but it still can't be safely made into a dense list (at least not naively).
It should be noted that PyPy already does unboxing Animats suggested, without any type annotation. At least for 4 years. It is not theoretical. https://morepypy.blogspot.com/2011/10/more-compact-lists-wit...