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+[:]+切片

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值