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 ++代碼的方法有很多( SIP , ctypes等)。
底線 (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+[:]+切片