Why is it possible to replace sometimes set() with {}?

前端 未结 4 556
北海茫月
北海茫月 2020-12-11 00:27

In PyCharm, when I write:

return set([(sy + ady, sx + adx)])

it says \"Function call can be replaced with set literal\" so it repl

4条回答
  •  情深已故
    2020-12-11 00:45

    It is an alternative syntax for set()

    >>> a = {1, 2}
    >>> b = set()
    >>> b.add(1)
    >>> b.add(2)
    >>> b
    set([1, 2])
    >>> a
    set([1, 2])
    >>> a == b
    True
    >>> type(a) == type(b)
    True
    

    dict syntax is different. It consists of key-value pairs. For example:

    my_obj = {1:None, 2:None}
    

提交回复
热议问题