11 ms·
Using attrs for everything in Python
- jgalt212 10y agoThis seems nice, but other than easy destructuring of functions that return long tuples (which really should return dicts), I cannot figure out a good use case for it over using a dictionary. I am keen to hear others' opinions.
- maxerickson 10y agoA class with more methods than the Point3d example.
- Myrmornis 10y agoYou'd have to expand more on your proposal for using a dictionary for people to be able to reply. The article was full of use cases, so why not address those rather than simply state that you can't think of any? When you say use a dictionary, do you mean subclass `dict`? One of the aims of the project is to constrain the available instance attributes, as in a normal class. How would your dict proposal do that?
- jgalt212 10y agoIn that case, I'd just deal with the overhead of a normal class. More boilerplate, of course, but I'd have more control over the class. As to the use cases, I was not really buying into them other than the nice destructuring example, which is actually very nice one.
- chc 10y agoI think the point is that this is basically a normal class, but one with the boilerplate already written for you. If you don't mind boilerplate, this library does nothing for you. If you don't want to write classes and think dicts are good enough, this library does nothing for you. If you do want to have classes but don't want the boilerplate that comes with making a basic-but-full-featured class, then that's where this library comes in.
- glyph 10y agoDictionaries are not for fixed fields. if you have a dict, it maps something to something else. You should be able to add and remove values. Objects, on the other hand, are supposed to have specific fields of specific types, because their methods have strong expectations of what those fields and types are. attrs lets you be specific about those expectations; a dictionary does not. It gives you a named entity (the class) in your code, which lets you explain in other places whether you take a parameter of that class or return a value of that class.
- whalesalad 10y agoThis is how I develop in Python. I do not use this helper library (and will certainly look into it), but definitely wish that more Python hackers built software in this way. Passing plain-old classes around that are very small and close to their domain is hugely valuable. Adding convenience methods or synthetic properties with a @property decorator is also hugely important. It lends itself to a very nice DSL where, for example, you can have image.png where the png property is a literal PNG representation of the image.
- stuaxo 10y agoThe short names confuse me a bit on this.
- hynek 10y agohttp://attrs.readthedocs.io/en/latest/overview.html#on-the-attr-s-and-attr-ib-names http://attrs.readthedocs.io/en/latest/overview.html#on-the-a...
- jredwards 10y agoI'm glad they recognize it, but I honestly don't even like the expansions.
- eliasdorneles 10y agoHello, Hynek! Thanks for attrs, wonderful library! I'd suggest to market the "serious business aliases" more prominently. It's not about aesthetics, but expectations: people like me are immediately puzzled by "attr.ib()", thinking "why ib?". The reason is because after reading a lot of Python code, our brain is already trained to recognize attributes and method names after the dot. Also, it's a reasonable expectation to be able to import a function or submodule and the code still make sense, but this won't make sense: from attr import s, ib @s class Thing(object): x = ib() I understand it looks such a small thing for you and others already used to this DSL, and also that it's not the most important technical aspect of the library. However, I do think it's an important human aspect of it. I have the feeling that by making the "no non-sense" alternative more prominent in the examples and documentation, it would reduce cognition steps for newbie users and maybe make your own life easier by not having to explain and paste this link every time someone finds it odd (which will probably continue to happen). Cheers!
- hynek 10y agoHello, thank you for the rare nice words in this thread! I got a bit hit by surprise by the submission of this article to HN (it’s like two weeks old now) so it caught me a bit off guard close before a new release and there’s some inconsistency between the GitHub README and the docs on RTD. The version that’s on PyPI now <https://pypi.org/project/attrs/> https://pypi.org/project/attrs/> has it as the first sentence after the first code sample anyone ever sees (and people still complained because the just don’t read :() and the new version <https://attrs.readthedocs.io/en/latest/> https://attrs.readthedocs.io/en/latest/> has a whole section around the example explaining attrs’ scope and ib/s: <https://attrs.readthedocs.io/en/latest/overview.html> https://attrs.readthedocs.io/en/latest/overview.html> (the new version also adds and promotes aliases that make more sense but that’s beyond the point). I feel like I’ve really done my due here and I’ll have to accept that I can’t make everyone happy. :|
- japhyr 10y agoWould there be any value to allowing a convention for the __init__() method that allows direct assignment of arguments to attributes? For example, replacing the following: class Point3D(object): def __init__(self, x, y, z): self.x = x self.y = y self.z = z with: class Point3D(object): def __init__(self, self.x, self.y, self.z): # Other initialization work It seems __init__ could be made to parse the list of parameters, and for any self.foo that's not defined, assign the corresponding argument to that attribute. But I have no experience in language design. Is there a reason this wouldn't work?
- m_ke 10y agoyou can just do class B: def __init__(self, a, b, c): self.__dict__.update(locals())
- jsmthrowaway 10y agoYou really, really, really shouldn't do this under any circumstances, though. If you find yourself messing with __dict__ you're down a hole, if you find yourself mutating it you're down an even deeper hole, if you find yourself calling locals() you probably forgot your shovel, and I'd hope that for all of that you'd have a better reason than minimizing code (that's way less readable, for example). I immediately get suspicious when I read code that interacts with __dict__ and I'm only sympathetic if you're cooking a metaclass or something equally arcane. __slots__ also makes __dict__ nonexistent.
- chc 10y agoWell, yeah, but you could implement it without __dict__ just by doing something like: for attr, val in locals().items(): setattr(self, attr, val) It still feels pretty un-Pythonic either way.
- jsmthrowaway 10y agoBecause it is. There is zero reason to be clever here. Yeah, you have to be explicit in __init__. We are all used to it. The "a," "b," "c" examples are disingenuous though because you're often validating, enforcing types, converting, deriving other attributes... __init__ is documentation. I read it to understand what's going on.
- mkolodny 10y agoWhile 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.
- dlbucci 10y agoAccording to the article, that's not really a fair comparison. The first example would need functools and at least 3 more methods to get to the same functionality of the latter example, at which point I think the former is less readable. That said, I'm not a big fan of wrapping classes, or this library's weirdly-but-concisely-named functions. I guess I'll have to give this a try the next time I'm writing Python to see if it's that much of a boost.
- crdoconnor 10y agoThose 3 methods aren't at all necessary for most objects you'll create and use.
- mixmastamyk 10y agoAlso, read the post and looked over the docs, and don't see any explanation of what `.s` and `.ib` is supposed to mean (though I can figure out from context). Does anyone know what the letters stand for? Struct and instance b…???
- Myrmornis 10y agoHow does this compare to schematics? It avoids inheritance, whereas in schematics and similar projects you'd have to inherit from their `Model` class (and thus acquire methods you might not want).
- jgalt212 10y agoUntil your comment, I had never seen schematics. I poked around a bit and my initial impression is that it seems like marshmallow-- it's heavily focused on validation rather than reducing boilerplate.
- StavrosK 10y agoSchematics looks a bit like a less elegant version of schema. It does seem more focused on validation than attrs, I agree.
- asciihacker 10y agoSchema [1] does not appear to have any support for object initialization. [1] https://pypi.python.org/pypi/schema/0.6.2 https://pypi.python.org/pypi/schema/0.6.2
- deleted 10y ago[deleted]
- RubyPinch 10y agoIt feels a bit hampered by "bad" design decisions to me I think its safe to say that the current view of what decorators do is, a) filter things going to functions (so they error, or hit a cache), or b) register a function with another library (e.g. flask path decorator) This library does a lot more than that, so it seems that maybe a metaclass (which exists to mutate the class's creation) would be more appropriate? This seems to act as a __init__ replacement, but it would be cool to act as a __init__ assistant (in other words, run the attr.s __init__, then run the user defined __init__) The ordering of attrs matters a bit more than one would expect, not only does it define the attribute order for creating a new instance, it also defines the order of attributes when sorting against other classes. You would not want a id=attr.ib() at the top of the class definition, where it most makes sense! The ordering of attrs is done through the .ib() call itself (which gives itself a number, and increments a global counter). This is a way of doing it, and practically might be the best way (it won't lead to any inconsistencies within a class), but it still feels weird I personally won't use it, but I might make a competitor to it for personal use
- nbadg 10y agoAgreed. 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 if the problem you're trying to solve is "generic container classes take too long to get set up". I think, were I to write something like this, I would probably do it using a metaclass, and then inject an __init__ on the MRO between the programmer-defined __init__ and the actual super().__init__. That way, you could call super().__init__(arg1, arg2, arg3) for the automatic attribute setting. You could also hook this in to some metaclass-defined storage containers to automate the __repr__ implementation, etc.
- StavrosK 10y agoThere is one: >>> C2 = attr.make_class("C2", ["a", "b"]) >>> C2("foo", "bar") C2(a='foo', b='bar')
- rwcollins 10y agoThe problem with constructs like this one is that it looks nice at first glance, but adds a cognitive load to an ecosystem that already has about "20 ways to do it" for every single problem. Python is beginning to rival C++ in that respect.
- Pitarou 10y agoPlease don't tell the C++ community that! We really don't want to encourage them. To someone who uses Python every day, the extra cognitive load of `attr` is more than made up for by all the boilerplate it eliminates. But for beginners, behind-the-scenes magic is a real turn off. I think that making `attr` an optional library is a fair compromise, but perhaps it would be a good idea to add a note to the library reminding the programmer not to use it if the code is likely end up in the hands of novices.
- crdoconnor 10y ago>To someone who uses Python every day, the extra cognitive load of `attr` is more than made up for by all the boilerplate it eliminates. That's funny because the first thought I had when reading this was "this does not eliminate nearly enough boilerplate to justify using that much magic". >But for beginners, behind-the-scenes magic is a real turn off. It's a turn off for beginners and experts but a turn on for intermediates.
- mhneu 10y ago"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, anything with multiprocessing, anything with distributed programming -- you want to avoid using objects as small bags of data. (Namedtuples should have been a way to improve this, but they are implemented as a class, so they have serialization issues too.) Lists of data can be serialized easily. Lists of objects cannot. dill tried really really hard to fix this, but the problems that remain are problems created by the Python language spec.
- glyph 10y agoNone 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 pickle >>> pickle.dumps(many) "ccopy_reg\n_reconstructor\np0\n(c__main__\nMany\np1\nc__builtin__\nobject\np2\n Ntp3\nRp4\n(dp5\nS'things'\np6\n(lp7\ng0\n(c__main__\nThing\np8\ng2\nNtp9\nRp10\ n(dp11\nS'a'\np12\nI1\nsS'b'\np13\nI2\nsbag0\n(g8\ng2\nNtp14\nRp15\n(dp16\ng12\n I3\nsg13\nI4\nsbasb." >>> import json >>> json.dumps(attr.asdict(many)) '{"things": [{"a": 1, "b": 2}, {"a": 3, "b": 4}]}' >>>
- rspeer 10y ago...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.
- 10y ago
- stinos 10y agoI can't find it at the moment but wasn't there another solution to this problem where you could just populate an object at will (like matlab's struct for instance)? IIRC it was dict-based and allowed something like foo = SomePythonWizardry() foo.x = 1 foo.a.b = 2 print( foo ) #yields e.g. foo.x = 1, foo.a.b = 2 Or am I just dreaming? It would be all the goodness and none of the weird syntax.
- desdiv 10y agoYou're probably thinking of jsobject¹: from jsobject import Object foo = Object() foo.x = 1 foo.a = {} # foo.a.b will fail without this line foo.a.b = 2 print(foo) # prints: {'a': {'b': 2}, 'x': 1} ¹ https://pypi.python.org/pypi/jsobject/ https://pypi.python.org/pypi/jsobject/
- asciihacker 10y agoI think if you import mock you can create mock.Mock() instances that allow you to assign fields that way.
- Animats 10y agoIn the end, this is about defining a typed structure. "@attr" plus validator constraints is effectively a typed structure. Python got further with minimal declarations than any other mainstream language. Yet there's a demand for typed variables. This is going into future Python, although in a strange and clunky way, because types don't fit the syntax. We seem to be converging on a consensus on when to declare types. The breakthrough was "auto", in C/C++, which declares a variable as the type of the value to which it is initialized. This is the default in Go and Rust. Function formal parameters and structure slots have explicit type declarations, while local variables are usually implicitly typed. Python seems to be moving in this direction, but from the no-type-declarations direction. Unfortunately, because the language itself doesn't do this well, it's being kludged through decorator macros.
- alexchamberlain 10y agoThe latest version does allow you to decorate variables with types... Not sure its doing anything with them yet.
- Animats 10y agoIt'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 its arrays of uniform type and functions which operate on them, in Python. The PyPy compiler could then generate efficient code for them. This would make PyPy the primary Python compiler. But if type annotation isn't enforced in CPython, code won't port reliably to a compiler which enforces it.) [1] https://www.python.org/dev/peps/pep-0484/ https://www.python.org/dev/peps/pep-0484/
- weberc2 10y agoHow 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.
- skrause 10y agoIf you just want an object that stores a few attributes and you don't want to define a namedtuple, you can use types.SimpleNamespace in Python >= 3.3: https://docs.python.org/3.5/library/types.html#types.SimpleNamespace https://docs.python.org/3.5/library/types.html#types.SimpleN... It's a pretty unknown feature from the stdlib, but sometimes very useful.
- radarsat1 10y agoHaven't finished reading yet but I just wanted to say that this _really_ resonates with me... > You know what? I’m done. 20 lines of code so far and we don’t even have a class that does anything; the hard part of this problem was supposed to be the quaternion solver, not “make a data structure which can be printed and compared”. YES. I don't know much about attrs, but if it helps solve this, I'm in.
- asciihacker 10y agoAdded to the Python OOP catalog: https://github.com/metaperl/python-oop https://github.com/metaperl/python-oop
- carapace 10y agoThis is wonderful and cute as hell and please PLEASE don't ever use it in production code. ;-)
- glyph 10y agoWhy not? It's aggressively tested and benchmarked to ensure its correct and its performance impact is as close to zero as possible. (It's pretty close.)
- carapace 10y agotl;dr: I don't like metaprogramming. ---- Both of those things are very good, even great, but they don't speak to the issue: It's [bad] magic. In this case, it does at module-load-time what should have been done previously at build-time. (And yes, I know Python normally doesn't have build-time the way e.g. C does.) I'm working on a large production codebase right now where several of the cowboy-coders here have added all sorts of crazy metaprogramming and it's a PITA. Let me tell you a story. We had a class decorator that introspected the attributes of the class and added "constants" to the enclosing module providing named string values for each of the class's attributes. It lets us access dict versions of these objects (they're data models) like foo_as_dict[foo_module.SOME_ATTR_NAME_BUT_IN_CAPS] ... Good luck finding foo_module.SOME_ATTR_NAME_BUT_IN_CAPS in foo_module.py though, because it's not there. (and notice that if the model field name changes ALL of your uses of the "constant" have to be refactored to the new name too, so it's not really as helpful as it might seem in the first place.) And now every new developer has to ask, "Where are these coming from?" and we get to point out the magic extra-clever decorator. (Try to imagine the horrible wonderful "voodoo" that decorator encompasses... Imagine the innocent newbie encountering it.) We have a "service builder" thing that takes a bunch of arguments and build a web service object in memory at module-load time. No source for any of that machinery. A bug in the service gives you a traceback running through a bunch of weird generic meta-code. Did I mention the guy who originally wrote it bailed to a new job a month after he finished it? No one else knows how it works. We can read the code and reverse engineer it, of curse, but that's kind of B.S., no? A junior dev could debug the real service code if it existed, but the meta-code that generates the services is another challenge (that they shouldn't have and the company shouldn't have to pay them to overcome.) We had unittest that read a JSON file and built a test suite in memory to run a bunch of other tests. They finally let me re-write that to a build-time script that scans the exact same JSON files and emit plain-vanilla unittest modules, that then can be run as normal, and the dev/user can look at the actual code of the test, rather than the meta-code of the test-builder. The same problem applies to this attr package. If you're trying to trace into or debug code that uses attr you've got to know attr at least enough to be able to follow what it's doing. It's an in-joke. ;-) (P.S. I'm a big fan dude. Nice to interact with you. All respect.)
- jplahn 10y agoThis feels a little bit like Lombok in the Java world. Can anybody with experience with both comment on that? We 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.
- eliasdorneles 10y agoLombok is kinda bigger commitment than attrs, because it interfaces with Java compiler internals (last time I used it, it was only possible to compile with sun/oracle jdk). attrs seems lesser risk IMO, it's just a plain Python library, no other deps. If you like Lombok, I'd expect you'll love attrs. :)
- mpdehaan2 10y agoHmm, 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 are stuff you have to unroll if the behavior grows, versus stuff you just add to.
- ctoth 10y agoFrom the docs: (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).
- safewayclubcard 10y agoI would consider that a negative. > There should be one-- and preferably only one --obvious way to do it. Whats going to happen when all professional code is using `attr.attributes` but all the documentation uses `attr.s`? Anyway its a minor point. I personally think it is a poor decision when a programmer decides to be cute than clear.
- anateus 10y agoFor lightweight containers that improve on the built-in namedtuples consider using the excellent boltons library's namedutils: http://boltons.readthedocs.io/en/latest/namedutils.html http://boltons.readthedocs.io/en/latest/namedutils.html
- yladiz 10y agoWhile I do see the usefulness for the specific example, and while it does seem to reduce boilerplate which is really nice, how useful it is outside of the Point3D example? For example, how often do you actually gt/lt comparisons between classes, or equate classes when they're not mathematical constructs like this? And if you add an attribute that shouldn't be used in repr or comparison, you have to explicitly say that in the attribute declaration, meaning it's would be much less clear what's going on down the road -- as in it won't be apparent how the comparison works or why this is being printed the way it is -- which I don't like in a language like Python. It almost seems like you're trading language level boilerplate for this library's boilerplate, and while it may be less with this library it obfuscates what's going on at the expense of more explicit code. I 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.
- BerislavLopac 10y agoattrs seems to be all over the place, but I find Schematics (http://schematics.readthedocs.io/en/latest/ http://schematics.readthedocs.io/en/latest/) much better designed and more powerful.
- asciihacker 10y agoYes, Schematics is impressive. Thank you, I added it to my list of Python OOP extensions [1]. I wonder why you need to supply a dictionary to the object instead of keyword pairs. [1] https://github.com/metaperl/python-oop https://github.com/metaperl/python-oop
- BerislavLopac 10y ago> I wonder why you need to supply a dictionary to the object instead of keyword pairs. My understanding is that the main reason is to make constructors more flexible, with arguments like _raw_data_, _trusted_data_ etc. But it is trivial to write a simple helper function that would accept keyword pairs and build a model instance.
- 0xmohit 10y agoI love Python; it’s been my primary programming language for 10+ years and despite a number of interesting developments in the interim I have no plans to switch to anything else. "interesting" and "developments" are referred to as haskell and rust respectively in the original post. "A History of Haskell: Being Lazy with Class" [0] by Paul Hudak et al suggests that it emerged before 1990. See http://haskell.cs.yale.edu/wp-content/uploads/2011/02/history.pdf#page=7 http://haskell.cs.yale.edu/wp-content/uploads/2011/02/histor... the page in the report showing Haskell timeline. [0] http://haskell.cs.yale.edu/wp-content/uploads/2011/02/history.pdf http://haskell.cs.yale.edu/wp-content/uploads/2011/02/histor...
- davidmanescu 10y agohttps://xkcd.com/927/ https://xkcd.com/927/ comes to mind when I read this.