3

It is well known that python has no empty set literal. However, I would like to know if there is still a performant way of creating an empty set

these are a few other ways to create an empty set...

There is the pythonic way:

def f():
    return set()

There is also this nasty looking way by unpacking an empty tuple:

def g():
    return {*()}

You could also build a set with one item and then remove that item:

def h()
    s = {1}
    s.remove(1)
    return s

Trying to use the literal syntax to create a set will give you a dictionary:

def i()
    return {}

Interestingly, the results of dis for each function (Python 3) are as follows:

>>> dis(f)
  1           0 LOAD_GLOBAL              0 (set)
              2 CALL_FUNCTION            0
              4 RETURN_VALUE
>>> dis(g)
  1           0 LOAD_CONST               1 (())
              2 BUILD_SET_UNPACK         1
              4 RETURN_VALUE
>>> dis(h)
  2           0 LOAD_CONST               1 (1)
              2 BUILD_SET                1
              4 STORE_FAST               0 (s)

  3           6 LOAD_FAST                0 (s)
              8 LOAD_METHOD              0 (remove)
             10 LOAD_CONST               1 (1)
             12 CALL_METHOD              1
             14 POP_TOP

  4          16 LOAD_FAST                0 (s)
             18 RETURN_VALUE
>>> dis(i)
  2           0 BUILD_MAP                0
              2 RETURN_VALUE

Looking at the disassemby, the pythonic way (function f) requires two instructions, first a LOAD_GLOBAL (which can be converted to a LOAD_FAST if you provide set as an argument with default value of set), and then the function call.

Creating a dict takes only one instruction, BUILD_MAP.

I can see from h that the set comprehension uses an equivalent instruction to BUILD_MAP, BUILD_SET, except that it first loads the constant value of (1) into the stack.

Is there a way to get Python to generate the bytecode BUILD_SET with no elements in the stack, like how the dict comprehension generated BUILD_MAP in function i?

micsthepick
  • 562
  • 7
  • 23
  • 3
    What is the problem you are trying to solve where it matters whether `set()` uses 1 or 2 bytecode instructions? – mkrieger1 Dec 24 '20 at 23:15
  • 1
    It's not possible, since there's no "empty set" literal. – cs95 Dec 24 '20 at 23:18
  • Does this answer your question? [Empty set literal?](https://stackoverflow.com/questions/6130374/empty-set-literal) – mkrieger1 Dec 24 '20 at 23:20
  • Maybe [modifying python bytecode](https://stackoverflow.com/q/33348067)? – superb rain Dec 25 '20 at 00:04
  • There is no empty set literal, if that is what you are asking. This seems like much ado about nothing. In any case, byte code is purely an implementation detail. It can (and often does) change, minor version to minor version. – juanpa.arrivillaga Dec 25 '20 at 00:49
  • 3
    Note, in the original PEP 218 that added a built-in set object, the notation `{-}` was proposed for an empty set, but actually, set literals did not get added at all at the time, I believe. – juanpa.arrivillaga Dec 25 '20 at 00:56
  • 1
    the question is not looking for a nice pythonic set literal (but it would be nice if there was one), the reason for asking is that having a single instruction is most likely to be the most performant way of creating an empty set – micsthepick Dec 27 '20 at 02:47
  • @mkrieger1 make an empty set as fast as possible, it's as simple as that – micsthepick Dec 27 '20 at 02:49

0 Answers0