News for hackers. Stuff that matters.
Re:Using attrs for everything in Python by mhneu on Thursday August 25, 2016 @02:49PM (#12361416)
"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.

Re:Using attrs for everything in Python by glyph on Thursday August 25, 2016 @08:20PM (#12363470)
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 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}]}'
    >>>
Re:Using attrs for everything in Python by rspeer on Thursday August 25, 2016 @09:46PM (#12363819)
...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 by xapata on Thursday August 25, 2016 @10:39PM (#12363974)
Why would it need to recurse? The method probably just returns a copy of the instance's __dict__. Maybe updating it with the __slots__ and their values.
Re:Using attrs for everything in Python by rspeer on Friday August 26, 2016 @02:38AM (#12364629)
It would need to recurse because you want something you can encode in JSON, not a dictionary containing a list containing miscellaneous instances.

We have an example there of an 'attrs' instance, containing a list containing 'attrs' instances, and all of the instances turn into dictionaries.

Re:Using attrs for everything in Python by xapata on Thursday August 25, 2016 @10:34PM (#12363964)
That's a problem with pickle, not namedtuple. I assume you're complaining about not being able to pickle an instance of a locally defined class?