[Patches] [ python-Patches-917095 ] dict type concat function (original) (raw)
SourceForge.net noreply at sourceforge.net
Tue Mar 16 14:35:00 EST 2004
- Previous message: [Patches] [ python-Patches-917095 ] dict type concat function
- Next message: [Patches] [ python-Patches-873305 ] list.__setitem__(slice) behavior
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Patches item #917095, was opened at 2004-03-16 00:33 Message generated for change (Comment added) made by tjreedy You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=305470&aid=917095&group_id=5470
Category: Core (C code) Group: Python 2.4 Status: Open Resolution: None Priority: 5 Submitted By: troy melhase (troy_melhase) Assigned to: Nobody/Anonymous (nobody) Summary: dict type concat function
Initial Comment: Adds a function to dictobject.c that allows python expressions like:
d = {2:3} d += {4:5} d {2: 3, 4: 5}
and like:
d = {2:3} e = d + {6:7} e {2: 3, 6: 7}
A few points:
I don't know much C, and this patch is probably implemented much more appropriately by someone who does
I don't know if there's a good reason that the dict type doesn't already supply this; if that's the case, I'd be interested to know why not
Lib/test/test_builtin.py fails (as it should) after applying this patch
Please advise, and thanks!
Comment By: Terry J. Reedy (tjreedy) Date: 2004-03-16 14:34
Message: Logged In: YES user_id=593130
I am usually big on orthogonality, but it never occurred to me that dicts should be addible, although sets are. What should be the result of {1:2} + {1:3}? If not something like {1:set (2,3)}, the operator is not commutative.
The reference to sets and | is that | is used, I believe, for set union (addition) (and & for intersection). So, if there were to be a dict union operator, the symbol should be | rather than +.
Comment By: troy melhase (troy_melhase) Date: 2004-03-16 14:22
Message: Logged In: YES user_id=548370
adds new syntax but not new functionality
writing d.update(x) is clearer and more flexible -- in Py2.4, update() can take the same arguments as dict()
The same is true of list.extend and list.append. After years of writing python code, I still forget that I can't add dictionaries to one another, which for me at least, violates the principle of least surprise.
The patch doesn't provide the same behavior as the update method; when adding two dicts, a third dict is returned. Only in the case of augmented assignment is the update method similar.
Moreover, every other basic container-ish type supports addition of the same type (or similar type). It seems to me that the dict type is missing a feature, not the other way around.
- concept doesn't usefully extend to - and *
I could see it extending to -, but you're right that it doesn't extend to *.
- possibly confusing given the | operator is used for sets
I don't understand this point, could you elaborate?
- {1:4}+{1:7} is not commutative and unlike list addition, there is no underlying order that makes the non- commutativity obvious. IOW, the operator may introduce a new class of hard to find bugs.
But unlike list addition, dict addition is commutative:
d = {2:3} e = {4:5} d + e == e + d True
Thanks for looking; could you advise again?
Comment By: Raymond Hettinger (rhettinger) Date: 2004-03-16 06:08
Message: Logged In: YES user_id=80475
I recommend against this for several reasons:
adds new syntax but not new functionality
concept doesn't usefully extend to - and *
writing d.update(x) is clearer and more flexible -- in Py2.4, update() can take the same arguments as dict()
no use cases showing the + operator to be more expressive
possibly confusing given the | operator is used for sets
{1:4}+{1:7} is not commutative and unlike list addition, there is no underlying order that makes the non- commutativity obvious. IOW, the operator may introduce a new class of hard to find bugs.
If for some reason this gets approved, please assign back to me for implementation, documentation, and unittests.
You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=305470&aid=917095&group_id=5470
- Previous message: [Patches] [ python-Patches-917095 ] dict type concat function
- Next message: [Patches] [ python-Patches-873305 ] list.__setitem__(slice) behavior
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]