So, I know that this,
a = {} # dict
constructs an empty dictionary. Now, I also picked up that this,
b = {1, 2, 3} # set
creates a set. This can easily be verified, as,
>>>print(type(a))
<class 'dict'>
>>>print(type(b))
<class 'set'>
While I understand what it does, I fail to see why we use 'set notation' for empty dictionaries. I tried to find some more information about the logic behind this in the set and dict sections of the manual, but sadly, I got nothing out of it.
Could anyone explain to me why we do this in this way? Is it for historical reasons, or am I missing something blatantly obvious?
setis like adictwith keys but no values, and they're both implemented using a hash table. But yes, it's a little annoying that the{}notation denotes an emptydictrather than an emptyset, but that's a historical artifact. I've seen a suggestion that{:}should be an emptydictand{}an emptyset, but I doubt that'll ever be implemented, as it clashes with too much existing code. - PM 2Ring{}is an emptydictbecause Python has always haddicts, since they're fundamental to the way Python works: normal object attributes are stored in adict, and variables behave as if they are name:value pairs in adict(although they aren't necessarily implemented that way for efficiency reasons). Whereassets were added to the language quite a while later. (Of course, before then it was always possible to "fake" a set by using adictwith dummy values). - PM 2Ring