pycharm写python字典_PyCharm无法识别字典值类型

I have a simple code snippet where I set dictionary values as empty lists:

new_dict = {}

for i in range(1, 13):

new_dict[i] = []

Now, if inside the loop on the next line I would type new_dict[i] and add a dot, I expect PyCharm to show me a list of methods available for a list, but PyCharm fails to recognize the dictionary value type in this simple case:

Why is it happening and what can I do? Using PyCharm 2016.1.2, Python 2.7.10.

As a workaround, I can explicitly add a type hint, letting PyCharm know that new_dict is a dictionary where keys are integers and values are lists by adding a # type: dict[int, list] inline comment:

解决方案

Lets consider a slightly more complex scenario with a variable:

for i in range(10):

if i%2:

x = 3

else:

x = "hello"

x. #type hint for both int and str

In this case the last line will give us all the methods of ints and strs because PyCharm is able to detect that x will either be an int or a str

Now replace all the occurences of x with my_dict[i]:

my_dict = {}

for i in range(10):

if i%2:

my_dict[i] = 3

else:

my_dict[i] = "hello"

my_dict[i]. #no type hint :(

All of the same rules apply as above, we know (and PyCharm would be able to figure out) that my_dict[i] is either going to be an int or a str.

However what would you expect to happen if you were not initializing the dict?

def f(my_dict): #or even (my_dict:dict)

my_dict[1]. #cannot possibly expect a type hint

In this case there is no way to know what the values of the dict are other then adding an explicit annotation just like you would in your example case:

def f(my_dict:"dict[int,list]"):

my_dict[1]. #get all the list methods

This really reinforces some lines from The Zen of Python:

Explicit is better than implicit.

Readability counts.

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to do it.

(I include readability because the explicit type hint is a lot easier to read then a potentially buried assignment)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值