簡體   English   中英

為什么set不在本地,全局或vars詞典中

[英]Why set is not in locals, globals or vars dictionaries

當我嘗試檢查集合在當前本地范圍或全局范圍中是否可用時,總是出現以下錯誤。

>>my_set = set()

>>my_set in locals()
>>Traceback (most recent call last):

  File "<ipython-input-22-47b6756e3345>", line 1, in <module>
    my_set in locals()

TypeError: unhashable type: 'set'
>>my_set in globals()
>>Traceback (most recent call last):

  File "<ipython-input-22-47b6755f5503>", line 1, in <module>
    my_set in globals()

TypeError: unhashable type: 'set'
>>my_set in vars()
>>Traceback (most recent call last):

  File "<ipython-input-22-47b6755f9947>", line 1, in <module>
    my_set in vars()

TypeError: unhashable type: 'set'

如果set在任何這些詞典(局部變量,全局變量或vars)中都沒有,我可以在哪里檢查是否定義了set?

檢查時需要引用名稱。

>>> my_set = set()
>>> locals
<built-in function locals>
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'my_set': set([]), '__doc__': None, '__package__': None}
>>> 'my_set' in locals()
True
>>> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM