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.pswp.cn/news/392481.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392481.shtml
英文地址,請注明出處:http://en.pswp.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…