On Tue, Sep 17, 2019 at 8:19 PM Rupert Spann <rupert.spann@ska.ac.za> wrote:

related to:  pep-0584, PEP-0218, and ???

The request is that since dictionary keys can not be duplicated / are unique that they may behave the same as sets - and have the set operators actions and return sets.


This is a correct observation (as already mentioned by Anders).

eg.:

aSet = set('a','b','c','bar')
aDict = {'a':'aaa', 'b':'baa' , 'foo':fou' }
bDict = {'a':'ea', 'b':'bee' , 'foo':fuh', 'bar': 'drink' }

aSet | aDict | bDict
= set('a','b','c','foo','bar')

This can be already done by:
aSet | set(aDict.keys()) | set(bDict.keys())
 

bDict & aDict
=set('a','b','foo')

This a confusing way to write:
set(bDict.keys()) | set(aDict.keys())
 

bDict - aSet
=set('foo')

ditto.
 

aDict^bDict
=set('bar')

The dictionary class would support set filtering:
New_dict_from_set = { your_key: old_dict[your_key] for your_key in aSET }
e.g. a function such as: (taking the intersection)
aDict.Subdictionary(aSet)
={'a':'aaa', 'b':'baa'}

Filtering a dict (keys) by a set and producing a dict makes sense. (+1)
 

set(dictionary) would return the Keys (as a mutable).

This is again a confusing way to write:
set(dictionary.keys())
 

Adding an element to the mutable set would add a None as a value to the dictionary.

Now I am not sure to follow it anymore. 'set' should return a set, adding anything to it cannot change the original dict, from which the keys were used. If it does not return a set, then it is not set() anymore.
 

The "-=", "-", "+", "+=" would behave as specified in pep-0584 if both are dictionaries, but if one of the operands is a set - a set is returned.

After seeing a bit of PEP-0584 I believe it faces some problems similar to your proposal -> the expected properties of those operators do not map correctly to the "dict space". E.g. '+' is not commutative, '-' what happens when the first operand is not "big enough" (ditto for '-='), etc.

The behaviour of
bDict.Subdictionary(aDict)
= {'a':'ea', 'b':'bee' , 'foo':fuh')


This is again a confusing way to write:
bDict.Subdictionary(set(aDict.keys())
 

i.e. treats aDict as a set


I think it is not possible to treat dict (object) as a set on an abstraction level. Imagine two dicts:
d1 = {"a": 1}
d2 = {"a": 2}

Which set operations could you define on those which would give a sensible result as a dict object (analogical to the set counterpart)? I believe that not so many. You can however define dict keys as sets and then use all set operations on them and then _interpret_ the results somehow as a dict while using the original d1 and d2 values. But the interpretation is not directly obvious from dicts alone, you have to add some other rules/logic for that.

Richard