NJU1healer的个人博客分享 http://blog.sciencenet.cn/u/NJU1healer

博文

Spyder的IDE中关于“from module import *” 警告与正反斜杠

已有 5700 次阅读 2020-8-22 15:43 |个人分类:Python|系统分类:科研笔记

(一)“from module import *”  警告

一般不建议使用from module import * ,最好是导入相对应的函数即可!

如果出现警告unable to detect undefined names,解决办法:

Your IDE is complaining, not Python. When you do from simple import *, you import everything exposed by simple. This is typically not recommended because it pollutes the global namespace and may implicitly overwrite an existing object.

You get a warning instead of an error because this behavior is not always bad. Having an __init__.py file that exposes objects from sub-modules is a very common pattern. As long as you understand the potential risks, just silence the warning:

from .input import *  # NOQA

If your modules don't expose many objects, just import them by name:

from .input import A, B, C

This has the benefit of allowing Python code analysis tools to better understand your code and warn you of potential issues.

关于#NOQA,意思如下:

        #NOQA 这个注释的作用是,告诉PEP8规范检测工具,这个地方不需要检测。也可以在一个文件第一行增加 #flake8:NOQA 来告诉规范检测工具,这个文件不用检查。

(二)正反斜杠

首先,“/”左倾斜是正斜杠,“\”右倾斜是反斜杠,可以记为:除号是正斜杠一般来说对应目录分隔符,Unix和Web用正斜杠/,windows的目录路径用反斜杠;如下图所示:
url:

 

windows目录路径:

 

(1)目录中的斜杠

python读文件需要输入的目录参数,列出以下列子:

path1 = r'c:\BaiduYunDownload\temp\readme.txt'

path2 = r'C:\BaiduYunDownload\temp\readme.txt'

path3 = R'c:\BaiduYunDownload\temp\readme.txt'

path4 = 'c:\\BaiduYunDownload\\temp\\readme.txt'

path5 = 'c:/BaiduYunDownload/temp/readme.txt'

打开文件函数open()中的参数可以是path1,也可以是path2、path3、path4、path5

path1、path2、path3的“\”为字符串中的特殊字符,加上R或r后变为原始字符串,则不会对字符串中的“\t”、“\r”进行字符串转义,path2中windows盘符大小写无关系,运行结果如下图所示:

  运行结果:

  

 pyth4:用一个“\”取消第二个“\”的特殊转义作用,即为“\\”

 

 运行结果:

path5:用正斜杠做目录分隔符也可以转到对应目录,并且在python中path5的方式也省去了反斜杠\转义的烦恼

  运行结果:

  

(2)正则表达式中的斜杠

正则表达式匹配反斜杠“\”,为什么是“\\\\”或是r“\\”呢?

因为在正则表达式中【\】为特殊符号,为了取消它在正则表达式中的特殊意义,需要加一个【\】就变成了\\,但是问题又来了,【\】也是字符串中的特殊字符,所以又要分别对两个【\】取消其特殊意义,即为【\\\\】。python中有一个原始字符串操作符,用于哪些字符串中出现特殊字符,在原始字符串中,没有转义字符和不能打印的字符。这样就可以取消了【\】在字符串中的转义功能,即r"\\"。

(3)换行符

写了续行符之后,续行符后面什么都不能 出现,必须换行(必须换行写内容)!

image.png

具体参见博客:https://blog.csdn.net/g_66_hero/article/details/78745608


【参考】

https://stackoverflow.com/questions/50439309/spyder-ide-complaining-about-unable-to-detect-undefined-names

https://www.cnblogs.com/baiyanhuang/p/3855841.html

https://www.jianshu.com/p/7b743742af53

https://www.cnblogs.com/pengjt/p/11528519.html

点滴分享,福泽你我!Add oil!



https://wap.sciencenet.cn/blog-3428464-1247440.html

上一篇:Python中定义类时的self与匿名函数
下一篇:Python中OrderedDic与sorted和lambda函数结合使用
收藏 IP: 211.162.81.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

全部作者的其他最新博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-25 23:43

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部