python+[:]+切片_我从C ++到Python的方式:概念上的改变

python+[:]+切片

by asya f

由asya f

我从C ++到Python的方式:概念上的改变 (How I went from C++ to Python: a conceptual change)

介绍 (Introduction)

People say that coding in Python is so easy that even a 6 year old can do it. This was the thought that I had in mind when I started coding in Python at work. I had been a full-time software developer for 4 years at that time, writing mainly in C++ on Linux, heavily using the QT library. However, I wrote bad Python code at first.

人们说用Python编码非常容易,甚至6岁的孩子也可以做到。 这是我在工作中开始使用Python进行编码时所想到的想法。 那时,我已经是一名全职软件开发人员已有4年了,主要使用QT库在Linux上用C ++编写程序。 但是,我起初编写了错误的Python代码。

It has been around 3 years since I’ve made that switch and I think that it’s a good point to sum-up the progress that I’ve made during this time. When looking back, I didn’t only change my main programming language, but also changed my work environment and the way I think about code.

自从进行此更改以来,已经过去了大约三年时间,我认为这是总结这段时间取得的进展的好时机。 回想起来,我不仅改变了我的主要编程语言,还改变了我的工作环境和代码思考方式。

I won’t go into the details and differences between C++ and Python, as there are numerous resources online, but rather will describe my own experience. I hope that this post would be useful for people going through the same transition that I did.

由于网上有大量资源 ,因此我将不介绍C ++和Python之间的细节和区别,而只是描述我自己的经验。 我希望这篇文章对经历与我相同过渡的人们会有所帮助。

C ++潜水,Python浮潜 (C++ is diving, Python is snorkeling)

C++ feels like diving into the magical mysteries of the sea - it is beautiful, but requires more learning and practice, and overall, the distance that you cover is not that big. Python is a bit like snorkeling - you see the beauty as soon as you stick your head into the water, but you don’t go much further down. You keep on swimming in shallow waters, and can cover a long distance easily. From this description it is clear, that each of these languages should be used at its right place and time.

C ++感觉像是潜入大海的神奇奥秘-它很漂亮,但是需要更多的学习和实践,总的来说,您所覆盖的距离并不那么大。 Python有点像浮潜-只要将头伸入水中,您就会看到美丽,但是您走得更远。 您可以继续在浅水中游泳,并且可以轻松地进行长距离游泳。 从该描述中很明显,应该在正确的时间和地点使用每种语言。

深入C ++并生存下来 (Diving into C++ and surviving it)

C++ is more strict and penalizes you more harshly on your mistakes. It’s not an effective coding session if you haven’t gotten a surprising Segmentation fault at least once. Therefore, it requires a deeper understanding of the computer, the compiler and the language. When you go deeper, you can really see and be impressed by beautiful things, like the compilation process and memory management.

C ++更加严格,对您的错误将给予更严厉的惩罚。 如果您至少一次没有遇到意外的Segmentation错误 ,那么这不是有效的编码会话。 因此,它需要对计算机,编译器和语言有更深入的了解。 当您深入研究时,您会真正看到美丽的事物并对其印象深刻,例如编译过程和内存管理。

As a C++ programmer I cared more about syntax tweaks and weird examples. I always knew where I allocated memory and how I was releasing it. The programs I wrote were more stand-alone as I preferred to know what was happening inside my code. The main idea was, that code that someone else wrote was less reliable, more error-prone, and might blow-up your memory usage.

作为C ++程序员,我更关心语法调整和奇怪的示例。 我一直都知道我在哪里分配内存以及如何释放内存。 我编写的程序更独立,因为我更想知道代码内部发生了什么。 主要思想是,其他人编写的代码可靠性较差,更容易出错,并且可能会消耗您的内存。

My main everyday tools were Vim with numerous plugins for writing code, GDB for debugging and Valgrind for analyzing my memory usage and errors. I compiled with g++ and wrote my own Makefiles. Back then, I didn’t feel that an IDE would benefit me, but would rather slow things down and make me lose touch with my code. In retrospect, I relied heavily on the compiler for finding my type errors.

我主要的日常工具是Vim,它具有用于编写代码的大量插件,用于调试的GDB和用于分析内存使用情况和错误的Valgrind 。 我用g ++编译并编写了自己的Makefiles 。 那时,我并不认为IDE能使我受益,而是想放慢脚步,使我与代码失去联系。 回想起来,我严重依赖编译器来查找类型错误

用Python浅游 (Shallow swimming in Python)

One of the first things that you need to learn when switching to Python is how to let go - you don’t know what is happening under the hood, where memory is allocated and released, and it’s OK. You are also encouraged to use code that was written by others, packed into libraries, as it saves you time and helps you code faster. It doesn’t mean that you need to write code that is dog-slow and relies on unmaintained and nonfunctional libraries, but the focus is definitely different.

切换到Python时,您需要学习的第一件事就是如何放手-您不知道引擎盖下发生了什么,分配和释放内存的位置,而且还可以。 还鼓励您使用其他人编写的代码,这些代码打包在库中,因为它可以节省您的时间并帮助您更快地编写代码。 这并不意味着您需要编写速度慢并且依赖于未维护且不起作用的库的代码,但是重点肯定是不同的。

When I started coding in Python, I first wrote C++ code in Python. It worked, but I didn’t gain any benefit from the language. My coding improved when I started writing in a more “Pythonic” fashion and started using libraries, and more advanced concepts such as generators, decorators and contexts.

当我开始用Python进行编码时,我首先用Python编写了C ++代码。 它奏效了,但是我从语言中得不到任何好处。 当我开始以更“ Pythonic”的方式编写代码并开始使用库以及更高级的概念(例如生成器 , 装饰器和上下文)时,我的编码得到了改善。

As a Python developer I tend to look first for the library that solves the issue at hand. Python has a rich library ecosystem and community that supports it. There are libraries for doing practically anything. Here are some handy ones that I use on a daily basis: NumPy for numerical calculations, OpenCV for computer vision, json for reading json files, SciPy for scientific calculations, sqlite3 for databases.

作为Python开发人员,我倾向于首先寻找能够解决当前问题的库。 Python具有丰富的库生态系统和支持它的社区。 有可以执行几乎所有操作的库。 这里是一些便于那些我每天都在使用: NumPy的用于数值计算, OpenCV计算机视觉, JSON用于读取JSON文件, SciPy的 对于科学计算, sqlite3用于数据库。

My everyday tool is PyCharm (yes, an IDE) with IdeaVim plugin. I started using it mainly due to the fact that it’s a powerful debugger, which is much friendlier than the default Python debugger, pdb. I also use pip for installing libraries that I need. I don’t monitor my memory usage anymore unless I really have to.

我的日常工具是带有IdeaVim插件的PyCharm (是的,一个IDE)。 我之所以开始使用它,主要是因为它是一个功能强大的调试器,它比默认的Python调试器pdb更友好。 我还使用pip来安装所需的库。 除非确实需要,否则我不再监视内存使用情况。

一些实用技巧 (Some Practical Tips)

If you are a C++ developer, and you consider starting to code in Python, here are my advice for you:

如果您是C ++开发人员,并且考虑开始使用Python进行编码,那么以下是我的建议:

  • Get rid of old habits — Stop using the C++ compiler as a debugger. Don’t over-optimize memory usage. Avoid writing C++-like code. And by all means, try not to rely on types.

    摆脱旧习惯 —停止将C ++编译器用作调试器。 不要过度优化内存使用率。 避免编写类似C ++的代码。 而且,请尽量不要依赖类型。

  • Get new habits — Start using libraries. Write Pythonic code (but don’t overdo it). Keep things readable. Use more complex concepts such as generators/decorators/contexts. Try PyCharm.

    养成新习惯 —开始使用库。 编写Pythonic代码(但不要过度使用)。 保持可读性。 使用更复杂的概念,例如生成器/装饰器/上下文。 尝试PyCharm。

  • Use C++ and Python common libraries — Some C++ libraries, like OpenCV and QT, have a Python interface. It is easy to start using the same library in Python rather than learning a new library from scratch.

    使用C ++和Python通用库 -某些C ++库(例如OpenCV和QT)具有Python接口。 开始在Python中使用相同的库很容易,而不是从头开始学习新的库。

  • Don’t forget your origins — Sometimes Python is just too slow or not optimal for the task. This is when your C++ knowledge kicks in. The are many ways (SIP, ctypes etc) to use C++ code inside Python.

    不要忘了您的起源 -有时Python太慢或不是最佳的任务。 这是您掌握C ++知识的时候。在Python中使用C ++代码的方法有很多( SIPctypes等)。

底线 (Bottom Line)

No matter what other people say, switching to a different programming language, especially to a language that is fundamentally different than the one you are used to, is not easy. Take the time to learn, to dig in, to discover. But most importantly, understand that not only the language should change, but also your coding style and work methodology.

不管别人怎么说,切换到另一种编程语言,尤其是切换到与您习惯上根本不同的语言,都不容易。 花时间学习,挖掘,发现。 但最重要的是,不仅要更改语言,还应更改编码风格和工作方法。

Good luck!

祝好运!

翻译自: https://www.freecodecamp.org/news/how-i-went-from-c-to-python-a-conceptual-change-8bf29d059428/

python+[:]+切片

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392481.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深度学习 免费课程_2020年排名前三的免费深度学习课程

深度学习 免费课程#1 Fastai面向程序员的实用深度学习2020 (#1 Fastai Practical Deep Learning for Coders 2020) On 21 of August 2020, fastai released the new version of the fastai library and of their Deep Learning course!2020年8月21日,fas…

mysql复制主从集群搭建

近期搭了个主从复制。中间出了点小问题,排查搞定,记录下来1环境:虚拟机:OS:centos6.5Linux host2 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/LinuxserverIP192.168.18.66192.168.18.6…

java jolt tuxedo_java通过jolt调用tuxedo服务.xls

java通过jolt调用tuxedo服务.xls还剩20页未读,继续阅读下载文档到电脑,马上远离加班熬夜!亲,喜欢就下载吧,价低环保!内容要点:?private bea.jolt.pool.servlet.ServletSessionPoolManager bool…

你的周末时光是什么样的?

周末是一个特殊的假日,隔三差五就会有,来的容易去得也容易,即便如此,我们还是应该好好珍惜,周末可以做的事太多了,既可以用来减压,也可以为下一周的学习和工作充电,不管做什么&#…

leetcode 290. 单词规律(hash)

给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循相同的规律。 这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应规律。 示例1: 输入: pattern “abba”, str “dog…

2019年微博用户画像_2019年您需要了解的有关用户的信息

2019年微博用户画像by Yisroel Yakovson通过伊斯洛尔雅科夫森 2019年您需要了解的有关用户的信息 (What You Need to Know About Your Users in 2019) Users have changed a lot in the last few years. We programmers may have a culture gap to overcome in this area. If …

使用lt;jsp:includegt;,不想写死URL,动态生成URL的解决的方法

JSP中文件包括有2种方式&#xff0c;静态包括和动态包括。静态包括使用<% include file"" %>。动态包括使用<jsp:include page"" />。本文不打算介绍这2种方式的差别和使用场景&#xff0c;主要关注page和file属性的路径问题。 假设事先知道被…

java udp ip端口 设置_UDP端口扫描Java只找到1个开放的UDP端口

我对端口扫描有一个分歧.我在Java中扫描一些IP地址的UDP端口.在我的程序中(假设一切正常)我只能找到一个开放的UDP端口.在另一方面端口扫描“nmap”我得到4个开放的UDP端口.有人可以告诉我为什么我不能通过Java代码找到多个端口&#xff1f;顺便说一句,我可以在我的代码中找到真…

pandas之Seris和DataFrame

pandas是一个强大的python工具包&#xff0c;提供了大量处理数据的函数和方法&#xff0c;用于处理数据和分析数据。 使用pandas之前需要先安装pandas包&#xff0c;并通过import pandas as pd导入。 一、系列Series Seris为带标签的一维数组&#xff0c;标签即为索引。 1.Seri…

机器学习:分类_机器学习基础:K最近邻居分类

机器学习:分类In the previous stories, I had given an explanation of the program for implementation of various Regression models. Also, I had described the implementation of the Logistic Regression model. In this article, we shall see the algorithm of the K…

leetcode 714. 买卖股票的最佳时机含手续费(dp)

给定一个整数数组 prices&#xff0c;其中第 i 个元素代表了第 i 天的股票价格 &#xff1b;非负整数 fee 代表了交易股票的手续费用。 你可以无限次地完成交易&#xff0c;但是你每笔交易都需要付手续费。如果你已经购买了一个股票&#xff0c;在卖出它之前你就不能再继续购买…

如何在Angular Material中制作自定义主题

by Charlee Li通过李李 如何在Angular Material中制作自定义主题 (How to make a custom theme in Angular Material) Angular Material is a great library that implements Material Design for Angular 2. The official document is sufficient regarding the component us…

最感叹的莫过于一见如故,最悲伤的莫过于再见陌路。最深的孤独,是你明知道自己的渴望,却得对它装聋作哑。最美的你不是生如夏花,而是在时间的长河里,波澜不惊。...

最感叹的莫过于一见如故&#xff0c;最悲伤的莫过于再见陌路。最深的孤独&#xff0c;是你明知道自己的渴望&#xff0c;却得对它装聋作哑。最美的你不是生如夏花&#xff0c;而是在时间的长河里&#xff0c;波澜不惊。转载于:https://www.cnblogs.com/dj258/p/7003890.html

java vimrc_.vimrc技巧

-------------------------------------------------------------------" 设置字符编码。参考&#xff1a;http://www.rainux.org/blog/index.php/2005/10/20/106" encoding: Vim 内部使用的字符编码方式&#xff0c;包括 Vim 的buffer (缓冲区)、菜单文" 本、消…

将PDF和Gutenberg文档格式转换为文本:生产中的自然语言处理

Estimates state that 70%–85% of the world’s data is text (unstructured data). Most of the English and EU business data formats as byte text, MS Word, or Adobe PDF. [1]据估计&#xff0c;全球数据的70&#xff05;–85&#xff05;是文本(非结构化数据)。 大多数…

Go_笔试题记录-指针与值类型实现接口的区别

1、如果Add函数的调用代码为&#xff1a; func main() {var a Integer 1var b Integer 2var i interface{} &asum : i.(*Integer).Add(b)fmt.Println(sum) } 则Add函数定义正确的是&#xff08;&#xff09; A.type Integer int func (a Integer) Add(b Integer) Intege…

leetcode 48. 旋转图像

解题思路 将数组从里到外分为若干层&#xff0c; 数组 [1,2,3], [4,5,6][7,8,9]的最外层即为 [1,2,3] [4 6][7,8,9] &#xff0c;将一层分为4条边&#xff0c;如741 123&#xff0c;将741放到123的位置&#xff0c;123放到369的位置&#xff0c;如此类推&#xff08;但是放置的…

如何恢复误删的OneNote页面

今天不小心把半个月的日记删掉了&#xff01;&#xff08;为了减少页面数量&#xff0c;每个月的日记会放在同一个页面上&#xff09;。 幸运的是OneNote有自动备份功能&#xff0c;喜极而泣。 操作方法来自微软支持 打开丢失了最近笔记的笔记本。 单击“文件”>“信息”&g…

javascript函数式_JavaScript中的函数式编程原理

javascript函数式After a long time learning and working with object-oriented programming, I took a step back to think about system complexity.经过长时间的学习和使用面向对象的编程&#xff0c;我退后了一步来思考系统的复杂性。 “Complexity is anything that mak…

java writeint_Java DataOutputStream.writeInt(int v)类型

DataOutputStream.writeInt(int v)方法示例DataOutputStream的DataOutputStream.writeInt(int v)方法具有以下语法。public final void writeInt(int v) throws IOException示例在下面的代码中展示了如何使用DataOutputStream.writeInt(int v)方法。import java.io.DataInputSt…