returning a namedtuple on dict.items()

Hi, That is my first post here, I hope it's the right place for it. so I was using some dicts had the following code: env = dict(t for t in os.environ.items() if 'SUBSTRING' not in t[0]) and I thought: It could have really been nice if i could do: env = dict(t for t in os.environ.items() if 'SUBSTRING' not in t.key) now I see that environ is not exactly a dict, but i think this is useful for all dictionary types. on the other hand, dicts are everywhere and i'm not sure about the performance impact of this for those that just ignore the access by attribute. of course, a matching t.value must be in place as well. ;) I would have opened a PR just to try out, but I feel this is so in the core that I'd first like to hear some comments. I was searching if anyone else had this idea before but couldn't find anything. Thanks, Nadav

31.03.20 20:15, Nadav Wexler пише:
This idea was proposed multiple times and was rejected. Python is optimized for raw tuples, it is one of most important type. Many operations with tuples are much faster than operations with named tuples (including creating and destroying). Tuples emitted by dict.item() (an enumerate(), and zip()) are usually short-living and produced in mass. So replacing tuples with named tuples would harm a lot of code which do not need attribute access.

On 31Mar2020 22:28, Nadav Wexler <blop.blopy@gmail.com> wrote:
that's what I assumed... thanks for the reply! :)
Also, there are modules to provide this kind of thing when you want it. For example, my cs.mappings module: https://pypi.org/project/cs.mappings/ has dicts_to_namedtuples() which converts an iterable of dicts to namedtuples, and the lower level function named_row_tuple(*keys) function used to create the specific namedtuple class from some keys (eg your_dict.keys()). There's also named_column_tuples() which presumes the column/attribute names come from the first data row. I wrote these when processing some data received as a spreadsheet, whose column names (from the human typed header row) tended to be stable, but the number and ordering of the columns were undependable. Feeding them through these (via an additional layer in cs.csvutils) got me nice tuples with readable attribute names. I'm sure there must be other modules with similar facilities as well. Cheers, Cameron Simpson <cs@cskk.id.au>

31.03.20 20:15, Nadav Wexler пише:
This idea was proposed multiple times and was rejected. Python is optimized for raw tuples, it is one of most important type. Many operations with tuples are much faster than operations with named tuples (including creating and destroying). Tuples emitted by dict.item() (an enumerate(), and zip()) are usually short-living and produced in mass. So replacing tuples with named tuples would harm a lot of code which do not need attribute access.

On 31Mar2020 22:28, Nadav Wexler <blop.blopy@gmail.com> wrote:
that's what I assumed... thanks for the reply! :)
Also, there are modules to provide this kind of thing when you want it. For example, my cs.mappings module: https://pypi.org/project/cs.mappings/ has dicts_to_namedtuples() which converts an iterable of dicts to namedtuples, and the lower level function named_row_tuple(*keys) function used to create the specific namedtuple class from some keys (eg your_dict.keys()). There's also named_column_tuples() which presumes the column/attribute names come from the first data row. I wrote these when processing some data received as a spreadsheet, whose column names (from the human typed header row) tended to be stable, but the number and ordering of the columns were undependable. Feeding them through these (via an additional layer in cs.csvutils) got me nice tuples with readable attribute names. I'm sure there must be other modules with similar facilities as well. Cheers, Cameron Simpson <cs@cskk.id.au>
participants (4)
-
Cameron Simpson
-
jdveiga@gmail.com
-
Nadav Wexler
-
Serhiy Storchaka