search
HomeBackend DevelopmentPython Tutorialpycharm 使用心得(七)一些实用功能介绍

实时比较

PyCharm 对一个文件里你做的改动保持实时的跟踪,通过在编辑器的左侧栏显示一个蓝色的标记。



这一点非常方便,我之前一直是在Eclipse里面用命令“Compare against HEAD” 来比较一个文件前后的改动。

在PyCharm中,你可以对你的改动一览无余。同时,点击那个标记,会显示出之前的内容以及一个工具栏:


你可以很容易地回滚这些改动,在一个详尽的对话框中查看这些改动或是将之前的文本粘贴到剪切板上。

提交时的比较预览
在Eclipse 中,当你提交改动时,会有一个提交对话框向你展示将要被提交的文件列表。

你可以双击其中任意一项来查看改动的对比:这时候,如果你看到代码里一些想修改的地方(比如一个拼错的单词,一段

遗漏的文档,等等),那可就不爽了:你必须关闭所有的东西(包括带有你精心写好的提交注释的提交框),去找到那处惹人烦的代码,改正,然后从头再来一遍。

PyCharm 有同样的特性,但是你的改动是可编辑的。我简直不能再去强调可以当场修正那些错误有多么棒啦!

在提交过程中,我可以很容易地修复像上面那些类似的拼写错误。

一个额外好处是,无论你因为何种原因关闭了提交对话框,当再次尝试提交时,它会保留你刚才填写的那些提交注释。

提交时检查
在提交过程中,我们还可以在实际提交前执行其他一些选项,像“优化导入(排序并移除未使用过的导入)“,检查变动集合中的待完成项等等。

一个有意思的地方是”执行代码分析“那一项。。。

代码审查
你可以让PyCharm在一个文件,目录或整个项目里执行“代码审查”操作。它会在无需实际执行程序的情况下,

找出存在问题以及相应的改进办法,比如类型检查,可静态的方法,违反代码规范等等问题。

正如在上一节中解释过的,这也可以在提交时在发生改动的文件中自动完成。这是一个文件一次审查之后的结果

 对于其中一些审查结果来说,你还可以应用一个修复建议。例如,对于“Function call can be replaced with set literal()”这一建议,我们可以选择相应的选项来修复。

PyCharm 会将这句::

代码如下:


xtensions = set(['.avi', '.mp4', '.mpg', '.mkv'])

自动替换为:

代码如下:


extensions = {'.avi', '.mp4', '.mpg', '.mkv'}

你可以关闭任何你不希望的审查,包括在项目一级。
重构

我一直在试图重命名一个模块,可在菜单项里一直找不到那一项。。。F2也无济于事。

突然我注意到文件子菜单下面的”重构/重命名“一项,就想。。。会是它吗?果然就是!

在你重命名一个模块时,PyCharm会询问你是否需要自动修改所有与之关联的项目。有福了!

还有很多其他的重构项目,但目前为止我还没有机会一一尝试。
关于过时代码的提示以及改进建议

PyCharm可以告诉你代码里面过时的结构块,并且提出一些改进建议。比如,我有像下面一段这样的代码:

代码如下:


with nested(open(filename1), open(filename2)) as (f1, f2):
 

PyCharm会明显的标注出nested:

我用的是Python2.7,事实上你可以使用嵌套上下文管理器,但是,唉,我还得支持python的一些老版本,就保留了nested。

但PyCharm提醒了我,这个事情本身还是很酷的。

任务+特性 分支
如同Eclipse一样(需要Mylin支持),PyCharm也支持基于任务的工作流(包括任务感知的上下文)。

然而PyCharm本身自带了很多直接可用的连接器(GitHub,Mantis,Jira,Bugzilla,等等)。

而且,当你开启一项任务时,它会询问你是否想要创建那个任务的一个特性分支,分支的名字可以配置。


这就避免了必须手动创建一个新的分支,而这个操作确实挺繁琐的(拿到任务的ID,选择名称,等等)。

这个是加分项。

快速
我观察到PyCharm在执行代码的自动完成时扫瞄代码的速度要远快于Eclipse,而且还不打断你当前的工作。

当你想保存一个文件时,Eclipse会阻止你这么做因为它正在后台执行一个任务,这有多惹人烦。。。

快速查看文档
当你把光标挪到一个函数、方法,类或者别的什么上的时候,你会得到一个选项来浏览”快速文档“。看一眼:

正如你看到的,它会用一种赏心悦目的格式展示方法的文档说明,还从代码使用说明中提示了参数的类型。。。即使该函数没有对应的文档说明,你还是会得到像下面这样的一个说明:

很好!

Docutils 支持
PyCharm 还包括了对于doctils的一流支持,这让它很容易从集成开发环境中直接再生成文档。

另一方面,它还包括了一个非常好的富文本(译者注:ReST即reStructuredTexteditor)编辑器(比Eclipse的IMHO好用的多):

完善的插件系统
跟据我的使用经验,在Eclipse里面安装插件是件挺痛苦的事情:

 很难找到你想要去的地方(提示: 帮助/安装新软件。。。);
你没有一个单一的索引,所以你得去网上去找那些插件源;
系统有时会因为库的依赖问题而崩溃,结果我就没装上那些我想要的插件(工作中还有其他人也同样遇到这个问题,最后不得不放弃);
PyCharm的插件体验则要流畅地多。

首先,它位于一个你认为它有可能会存在的地方:就在”设置”下面的一项:


你可以很容易地浏览插件:



我快速安装了一个标记编辑器(和ReST一样),一个临时存放代码片段的插件,可以让我直接从选择的文本中快速地剪贴,并且预览代码,和SublimeText类似,还用一个漂亮的代码外观。

结语
目前为止就这些了。我会向我的朋友尽力展示一切我认为“看这有多酷”的事情。我使用PyCharm的时间很短,如果我发现还有更多有趣的事情值得再写一篇发表的话,我会继续写。














 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software