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

前端 未结 4 560
北海茫月
北海茫月 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

    set([iterable]) is the constructor to create a set from the optional iterable iterable. And {} is to create set / dict object literals. So what is created depends on how you use it.

    In [414]: x = {}
    
    In [415]: type(x)
    Out[415]: dict
    
    In [416]: x = {1}
    
    In [417]: type(x)
    Out[417]: set
    
    In [418]: x = {1: "hello"}
    
    In [419]: type(x)
    Out[419]: dict
    

提交回复
热议问题