最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】PyCharm中Python代码提示:Shadows name from outer scope

PyCharm crifan 7633浏览 0评论

对于PyCharm中提示Python代码:

Shadows name from outer scope

This inspection detects shadowing names defined in outer scopes

之前一直没去在意,直到:

调试时发现,貌似这个shadows name,其实就是:

多余的变量,此处字典变量的指针,而不是实体

所以会出现,后续给

processedInfoDict[“orderNumber”] = 0

会影响到之前的:

productInfoDict

所以此时再去搞清楚,Python中shadows name到底是啥意思

pycharm python Shadows name from outer scope

python – How bad is shadowing names defined in outer scopes? – Stack Overflow

好像是:

外部传入的变量的名字,和函数内部的变量名是一样的?

是不好的做法?

python – Shadows name xyz from outer scope – Stack Overflow

scope – Python name shadowing confusion – Stack Overflow

What does “shadow name “function” by outer scope” means?:learnpython

(1)Python作用域的问题 – SegmentFault

python Shadows name from outer scope

Py3K: Solving the “outer scope” problem | Unspecified Behaviour

PEP 3104 — Access to Names in Outer Scopes | Python.org

shadows name from outer scope python

python – Confusing variable scope (shadowing variable from outer scope) – Stack Overflow

Python程序员常犯的10个错误 | 书影博客

此处看了半天,感觉还是:

由于是字典变量的指针,所以属于多此一举,就像影子的感觉

-》所以估计解决此处指针问题,就解决了shadow的警告了?

去试试。

python dict reference

python 字典变量 引用 拷贝

Dictionaries — PySchools Python Quick Reference Guide

assigning value in python dict (copy vs reference) – Stack Overflow

dict — Python Reference (The Right Way) 0.1 documentation

copy — Python Reference (The Right Way) 0.1 documentation

感觉应该用:

dict. copy()

的确是:用了copy后,解决了dict变量引用的问题:

不过还是有shadows name的警告:

然后去把函数调用的外部outer的代码:

for (curIdx, eachProductDict) in enumerate(curLoopProductList):

    logging.info(“[%s] eachProductDict=%s”, curIdx, eachProductDict)

    (processedInfoDict, toProcessInfoDict) = orderSingleMsStoreProduct(driver, gCfg, eachProductDict)

    logging.debug(“processedInfoDict=%s, toProcessInfoDict=%s”, processedInfoDict, toProcessInfoDict)

    processedOrderTime = processedInfoDict[“orderNumber”]

    toProcessOrderTime = toProcessInfoDict[“orderNumber”]

    logging.info(“Processed [%s] times, [%s] times to process, for product [%s]”,

                 processedOrderTime, toProcessOrderTime, processedInfoDict[“productName”])

    nextToProcessProductList.append(toProcessInfoDict)

    logging.debug(“nextToProcessProductList=%s”, nextToProcessProductList)

改为:

for (curIdx, eachProductDict) in enumerate(curLoopProductList):

    logging.info(“[%s] eachProductDict=%s”, curIdx, eachProductDict)

    (hasProcessedInfoDict, needToProcessInfoDict) = orderSingleMsStoreProduct(driver, gCfg, eachProductDict)

    logging.debug(“processedInfoDict=%s, toProcessInfoDict=%s”, hasProcessedInfoDict, needToProcessInfoDict)

    processedOrderTime = hasProcessedInfoDict[“orderNumber”]

    toProcessOrderTime = needToProcessInfoDict[“orderNumber”]

    logging.info(“Processed [%s] times, [%s] times to process, for product [%s]”,

                 processedOrderTime, toProcessOrderTime, hasProcessedInfoDict[“productName”])

    nextToProcessProductList.append(needToProcessInfoDict)

    logging.debug(“nextToProcessProductList=%s”, nextToProcessProductList)

就消除了警告了:

【总结】

此处,函数内部的变量,如果和函数被调用的外部的变量一样的话,就被PyCharm中叫做shadows name

这样的话,容易引发不容易觉察到的,由于函数内部和外部的变量名一致而引发的一些问题:

比如:内部函数名引用时不小心写错了时,就会导致其实调用了外部变量名,从而导致逻辑错乱。

所以解决办法是:

确保函数内部和外部的变量名不要重复,这样就不会导致可能由此导致的错误了。

【后记】

看到:

copy — Python Reference (The Right Way) 0.1 documentation

<code>&gt;&gt;&gt; d = {'a': 1, 'b': [1, 2]}
&gt;&gt;&gt; dd = d.copy()
&gt;&gt;&gt; dd
{'a': 1, 'b': [1, 2]}
&gt;&gt;&gt; d['b'][0] = 'foo'      # since copy() returns a shallow copy
&gt;&gt;&gt; dd                     # (only references to the copied elements are returned),
{'a': 1, 'b': ['foo', 2]}  # altering the objects in original dictionary will affect it’s copy as well
</code>

说是dict.copy()是 shallow copy潜拷贝

-》修改了被引用的dict时,copy后的dict的值也变化!!!

-〉后续使用时候要注意这个问题,搞不好就导致值变了,还不知道呢。

转载请注明:在路上 » 【已解决】PyCharm中Python代码提示:Shadows name from outer scope

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
57 queries in 0.105 seconds, using 18.33MB memory