-3

T={}

Is T a set or a dictionary?

type(T) returns dict but isn't it an ambiguous notation? OR am my missing something? sys.getsizeof(T) gives 148 and I think this is just an anticipation of dict to be on the safer side.

Please share your thoughts.

Grisha Levit
  • 8,194
  • 2
  • 38
  • 53
Nic
  • 33
  • 4
    Just because sets in math are in `{}` doesn't mean it should be same way in Python – ᴀʀᴍᴀɴ Feb 01 '17 at 09:18
  • 4
    It is a little ambiguous, unless you're aware of the history. Python has always had dictionaries, but sets are a relatively late addition to the language, and set literals are even more recent. – PM 2Ring Feb 01 '17 at 09:19
  • 2
    If type(T) tells you that it's a dict, then it's a dict. Why would it lie to you? – Stefan Pochmann Feb 01 '17 at 09:24
  • 1
    Possible duplicate of [Empty set literal in Python?](http://stackoverflow.com/questions/6130374/empty-set-literal-in-python) – Mohammad Yusuf Feb 01 '17 at 09:26
  • 2
    This seems like you want a discussion. Stackoverflow was not designed for discussions, but for questions based on facts. If you want a discussion, you could try reddit.com – Martin Thoma Feb 01 '17 at 09:26
  • It might seem like sets are more fundamental than dicts, but bear in mind that sets are merely a convenient collection type, whereas dicts are vital to Python: they're used for `globals()`, `locals()`, and object attributes (unless the object defines `__slots__`). – PM 2Ring Feb 01 '17 at 09:32

2 Answers2

1

It's a dict. Period. If you want an empty set, use set().

If you want a filled set, you can use either the {1, 2, 3, 4} (since Python 3) notation or the set((1, 2, 3, 4)) notation.

Guillaume
  • 5,497
  • 3
  • 24
  • 42
1
d = {}  # A dictionary
d = dict()  # also a dictionary
s = set()  # A set

This is also stated in the docs:

Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.

Especially the examples afterwards make it clear. (To be honest, I'm a bit confused by the "Curly braces [...] can be used to create sets.")

Speculation: The {} notation is shorter and dictionarys are more often used. Also, it is a common notation in other languages, too.

One way to think about dictionaries is as a set of (key, value) tuples. Hence the set-like notation does make sense.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • 2
    Conversely, a way to think about sets is that they are dicts with keys but no values. – PM 2Ring Feb 01 '17 at 09:34
  • @PM2Ring I read somewhere that sets were actually originally built this way (as dicts without values) in CPython, is that right? – Chris_Rands Feb 01 '17 at 09:53
  • 2
    @Chris_Rands I'm not sure about that. However, when I first investigated the CPython source code for the set & dict objects there were a lot of similarities, although I expect that they've since diverged somewhat, since for Python 3.6 some rather radical changes have been made to the dict implementation. – PM 2Ring Feb 01 '17 at 09:58
  • @PM2Ring Thank you! That makes sense; and I've just found the text I was referring to: http://stackoverflow.com/a/3949350/6260170 – Chris_Rands Feb 01 '17 at 10:07
  • 1
    @Chris_Rands Nice work. And the changes to how dictobject.c does its hashtable are still very fresh: people were complaining only a few months ago that the comments were no longer in synch with the code, but that's since been fixed. :) See https://bugs.python.org/issue20674 I suppose we really need to ask one of the core devs, maybe [Raymond Hettinger](http://stackoverflow.com/users/1001643/raymond-hettinger) himself, to get the full story... – PM 2Ring Feb 01 '17 at 10:15