Python进阶路径-从学徒到大师

问题:Python进阶路径-从学徒到大师

我已经学习,工作和玩Python一年半了。随着生物学家逐渐转向生物信息学,这种语言一直是我在实验室中所做的所有重大贡献的核心。我或多或少地爱上了Python允许我表达漂亮的解决方案的方式,也喜欢这种语言的语义,这种语义允许从思想到可行的代码的自然流动。

我想知道的是您对在此论坛或其他论坛中很少见到的一种问题的回答。对我来说,这个问题似乎对Python改进道路上的每个人都很重要,但谁又想知道下一步应该做什么。

让我总结一下我不想先问的内容;)

  • 我不想知道如何快速学习Python
  • 我也不想找出了解该语言的最佳方法
  • 最后,我不想知道一种“一劳永逸”的方法。

我想知道您的看法是:

从徒弟身份到专家地位,您会建议Python入门者采取哪些步骤(无论您的专业知识要求如何,都可以随时停下来),以便一个人不断地提高自己,成为越来越优秀的Python程序员。SO上的某些人似乎因其Python的出色表现值得敬拜,请赐教:)

我喜欢的答案(但随时可以给读者群:P带来惊喜)的格式大致如下:

  • 阅读此内容(例如:python教程),请注意此类详细信息
  • 如此众多的时间/问题/代码行的代码
  • 然后,阅读此书(例如:这本或那本书),但是这次要注意这一点
  • 解决一些现实生活中的问题
  • 然后,继续阅读Y。
  • 一定要掌握这些概念
  • X时间代码
  • 回到这样的基础知识或进一步…
  • (你明白了:)

我真的很想知道您的意见,以便在不断发展的过程中(当然,要做出适当的努力)在各个阶段中应该注意的是什么。如果您来自特定的专业领域,请讨论在该领域中您认为合适的路径。

编辑:感谢您的大力支持,我又回到了Python改进的轨道上!我真的很感激!

I’ve been learning, working, and playing with Python for a year and a half now. As a biologist slowly making the turn to bio-informatics, this language has been at the very core of all the major contributions I have made in the lab. I more or less fell in love with the way Python permits me to express beautiful solutions and also with the semantics of the language that allows such a natural flow from thoughts to workable code.

What I would like to know is your answer to a kind of question I have seldom seen in this or other forums. This question seems central to me for anyone on the path to Python improvement but who wonders what his next steps should be.

Let me sum up what I do NOT want to ask first ;)

  • I don’t want to know how to QUICKLY learn Python
  • Nor do I want to find out the best way to get acquainted with the language
  • Finally, I don’t want to know a ‘one trick that does it all’ approach.

What I do want to know your opinion about, is:

What are the steps YOU would recommend to a Python journeyman, from apprenticeship to guru status (feel free to stop wherever your expertise dictates it), in order that one IMPROVES CONSTANTLY, becoming a better and better Python coder, one step at a time. Some of the people on SO almost seem worthy of worship for their Python prowess, please enlighten us :)

The kind of answers I would enjoy (but feel free to surprise the readership :P ), is formatted more or less like this:

  • Read this (eg: python tutorial), pay attention to that kind of details
  • Code for so manytime/problems/lines of code
  • Then, read this (eg: this or that book), but this time, pay attention to this
  • Tackle a few real-life problems
  • Then, proceed to reading Y.
  • Be sure to grasp these concepts
  • Code for X time
  • Come back to such and such basics or move further to…
  • (you get the point :)

I really care about knowing your opinion on what exactly one should pay attention to, at various stages, in order to progress CONSTANTLY (with due efforts, of course). If you come from a specific field of expertise, discuss the path you see as appropriate in this field.

EDIT: Thanks to your great input, I’m back on the Python improvement track! I really appreciate!


回答 0

我认为Python精通的过程是这样的:

  1. 发现列表理解
  2. 发现生成器
  3. 一体化的地图,减少,过滤器,ITER,范围,XRANGE经常到你的代码
  4. 发现装饰者
  5. 编写递归函数,很多
  6. 探索itertoolsfunctools
  7. 阅读真实世界Haskell免费在线阅读
  8. 用大量的高阶函数,递归等重写所有旧的Python代码。
  9. 每当他们的小伙伴向您展示Python类时,他们都会感到烦恼。声称它可以“更好”地实现为字典以及一些功能。接受函数式编程。
  10. 重新发现策略模式,然后从命令代码中发现所有这些事情,您很难在Haskell之后忘记。
  11. 找到平衡。

I thought the process of Python mastery went something like:

  1. Discover list comprehensions
  2. Discover generators
  3. Incorporate map, reduce, filter, iter, range, xrange often into your code
  4. Discover Decorators
  5. Write recursive functions, a lot
  6. Discover itertools and functools
  7. Read Real World Haskell (read free online)
  8. Rewrite all your old Python code with tons of higher order functions, recursion, and whatnot.
  9. Annoy your cubicle mates every time they present you with a Python class. Claim it could be “better” implemented as a dictionary plus some functions. Embrace functional programming.
  10. Rediscover the Strategy pattern and then all those things from imperative code you tried so hard to forget after Haskell.
  11. Find a balance.

回答 1

扩展您的Python知识的一种好方法是挖掘您已经使用的库,平台和框架的源代码。

例如,如果您要在Django上构建网站,则可以通过查看Django如何实现相关功能来回答很多可能困扰您的问题。

这样,您将继续学习新的习惯用法,编码样式和Python技巧。(有些会好,有些会坏。)

并且,当您在源代码中看到一些您不了解的Pythony时,请跳至#python IRC频道,您会发现很多“语言律师”很乐于解释。

多年来,这些小小的澄清的积累导致对语言及其所有来龙去脉的更深入的了解。

One good way to further your Python knowledge is to dig into the source code of the libraries, platforms, and frameworks you use already.

For example if you’re building a site on Django, many questions that might stump you can be answered by looking at how Django implements the feature in question.

This way you’ll continue to pick up new idioms, coding styles, and Python tricks. (Some will be good and some will be bad.)

And when you see something Pythony that you don’t understand in the source, hop over to the #python IRC channel and you’ll find plenty of “language lawyers” happy to explain.

An accumulation of these little clarifications over years leads to a much deeper understanding of the language and all of its ins and outs.


回答 2

了解(更深入地)关于内存管理的Python数据类型及其作用

正如社区中的一些人所知道的,我教授Python类,其中最受欢迎的是综合性的Intro + Intermediate类以及介绍各种应用程序开发领域的“高级”类。

通常,我会被问到一个非常类似的问题:“我应该学习入门还是高级类?我已经对Python进行了1-2年的编程,我认为入门对我来说太简单了,所以我会想直接跳到高级类… 您会推荐哪门类?”

为了回答他们的问题,我探究了他们在这一领域的实力-不是说这确实是衡量他们是否准备好参加任何高级类的最佳方法,而是要了解他们对Python对象和对象的基本了解程度。内存模型,这是由不仅是初学者,而且还超出初学者的人编写的许多 Python错误的原因。

为此,我将他们指向这个简单的两部分测验问题:

很多时候,他们都能够获得输出,但是为什么做出响应会变得更加困难和重要……我将输出权衡为答案的20%,而“为什么”获得80%的信用。如果他们不能理解为什么,无论他们有什么Python经验,我都会始终引导人们参加综合性的入门+中级类,因为我在对象和内存管理上进行了一次讲座,以至于您应该能够回答这些问题。输出以及为什么具有足够的信心。(只是因为您知道1-2年后Python的语法并没有使您准备超越“初学者”标签,除非您对Python的工作原理有了更好的了解。)

要求类似答案的后续查询就更加困难,例如,

例子3

x = ['foo', [1,2,3], 10.4]
y = list(x) # or x[:]
y[0] = 'fooooooo'
y[1][0] = 4
print x
print y

我建议的下一个主题是很好地理解引用计数,了解“ interning”的含义(但不一定要使用它),了解浅层和深层副本(如上述示例3所示),最后是各种类型和类型之间的相互关系。语言的构造,即列表与元组,字典与集合,列表理解与生成器表达式,迭代器与生成器等;但是所有其他建议在另一时间都已发布。希望这对您有所帮助!:-)

ps。我同意其他与内省和研究其他项目的源代码更加亲密的答复,并在两个建议中都添加了强烈的“ +1”!

pps。顺便问一问。我希望我一开始有足够的才智来问这样的事情,但是那是很久以前的事了,现在我正尝试通过多年的全职Python编程来帮助其他人!

Understand (more deeply) Python’s data types and their roles with regards to memory mgmt

As some of you in the community are aware, I teach Python courses, the most popular ones being the comprehensive Intro+Intermediate course as well as an “advanced” course which introduces a variety of areas of application development.

Quite often, I get asked a question quite similar to, “Should I take your intro or advanced course? I’ve already been programming Python for 1-2 years, and I think the intro one is too simple for me so I’d like to jump straight to the advanced… which course would you recommend?”

To answer their question, I probe to see how strong they are in this area — not that it’s really the best way to measure whether they’re ready for any advanced course, but to see how well their basic knowledge is of Python’s objects and memory model, which is a cause of many Python bugs written by those who are not only beginners but those who have gone beyond that.

To do this, I point them at this simple 2-part quiz question:

Many times, they are able to get the output, but the why is more difficult and much more important of an response… I would weigh the output as 20% of the answer while the “why” gets 80% credit. If they can’t get the why, regardless how Python experience they have, I will always steer people to the comprehensive intro+intermediate course because I spend one lecture on objects and memory management to the point where you should be able to answer with the output and the why with sufficient confidence. (Just because you know Python’s syntax after 1-2 years doesn’t make you ready to move beyond a “beginner” label until you have a much better understanding as far as how Python works under the covers.)

A succeeding inquiry requiring a similar answer is even tougher, e.g.,

Example 3

x = ['foo', [1,2,3], 10.4]
y = list(x) # or x[:]
y[0] = 'fooooooo'
y[1][0] = 4
print x
print y

The next topics I recommend are to understanding reference counting well, learning what “interning” means (but not necessarily using it), learning about shallow and deep copies (as in Example 3 above), and finally, the interrelationships between the various types and constructs in the language, i.e. lists vs. tuples, dicts vs. sets, list comprehensions vs. generator expressions, iterators vs. generators, etc.; however all those other suggestions are another post for another time. Hope this helps in the meantime! :-)

ps. I agree with the other responses for getting more intimate with introspection as well as studying other projects’ source code and add a strong “+1” to both suggestions!

pps. Great question BTW. I wish I was smart enough in the beginning to have asked something like this, but that was a long time ago, and now I’m trying to help others with my many years of full-time Python programming!!


回答 3

查看Peter Norvig关于十年内成为一名程序员的文章:http : //norvig.com/21-days.html。我敢打赌它适用于任何语言。

Check out Peter Norvig’s essay on becoming a master programmer in 10 years: http://norvig.com/21-days.html. I’d wager it holds true for any language.


回答 4

了解内省

  • 写一个dir()等效的
  • 写一个type()等效的
  • 弄清楚如何“Monkey补丁”
  • 使用该dis模块查看各种语言构造如何工作

做这些事情会

  • 给您一些有关如何实现python的良好理论知识
  • 给您一些底层编程的良好实践经验
  • 使您对python数据结构有良好的直观感觉

Understand Introspection

  • write a dir() equivalent
  • write a type() equivalent
  • figure out how to “monkey-patch”
  • use the dis module to see how various language constructs work

Doing these things will

  • give you some good theoretical knowledge about how python is implemented
  • give you some good practical experience in lower-level programming
  • give you a good intuitive feel for python data structures

回答 5

def apprentice():
  read(diveintopython)
  experiment(interpreter)
  read(python_tutorial)
  experiment(interpreter, modules/files)
  watch(pycon)

def master():
  refer(python-essential-reference)
  refer(PEPs/language reference)
  experiment()
  read(good_python_code) # Eg. twisted, other libraries
  write(basic_library)   # reinvent wheel and compare to existing wheels
  if have_interesting_ideas:
     give_talk(pycon)

def guru():
  pass # Not qualified to comment. Fix the GIL perhaps?
def apprentice():
  read(diveintopython)
  experiment(interpreter)
  read(python_tutorial)
  experiment(interpreter, modules/files)
  watch(pycon)

def master():
  refer(python-essential-reference)
  refer(PEPs/language reference)
  experiment()
  read(good_python_code) # Eg. twisted, other libraries
  write(basic_library)   # reinvent wheel and compare to existing wheels
  if have_interesting_ideas:
     give_talk(pycon)

def guru():
  pass # Not qualified to comment. Fix the GIL perhaps?

回答 6

我会给您最简单,最有效的建议,我认为任何人都可以给您:code

通过编码,您只能更好地使用一种语言(这意味着理解它)。您必须积极享受编码,获得启发,提出问题并自己寻找答案。

有一个小时的空闲时间?编写将反转字符串的代码,并找出最佳解决方案。有空吗?为什么不尝试一些网络爬网。阅读其他人的代码。看看他们是如何做的。问问自己你会怎么做。

当我无聊的时候,我打开IDE并进行代码风暴。我记下了听起来有趣且充满挑战的想法。URL缩短器?当然可以。哦,我学会了如何将数字从一个底数转换为另一个底数!

无论您的技能水平如何,这都是有效的。您永不停止学习。通过在业余时间积极编码,您将花费很少的精力来理解该语言,并最终成为一名专家。您将积累知识和可重用的代码,并记住成语。

I’ll give you the simplest and most effective piece of advice I think anybody could give you: code.

You can only be better at using a language (which implies understanding it) by coding. You have to actively enjoy coding, be inspired, ask questions, and find answers by yourself.

Got a an hour to spare? Write code that will reverse a string, and find out the most optimum solution. A free evening? Why not try some web-scraping. Read other peoples code. See how they do things. Ask yourself what you would do.

When I’m bored at my computer, I open my IDE and code-storm. I jot down ideas that sound interesting, and challenging. An URL shortener? Sure, I can do that. Oh, I learnt how to convert numbers from one base to another as a side effect!

This is valid whatever your skill level. You never stop learning. By actively coding in your spare time you will, with little additional effort, come to understand the language, and ultimately, become a guru. You will build up knowledge and reusable code and memorise idioms.


回答 7

如果您正在使用python进行科学研究(看来您是在其中),那么其中的一部分将是学习和理解科学图书馆,对我而言,这将是

  • 麻木
  • 科学的
  • matplotlib
  • mayavi / mlab
  • 查科
  • 赛顿

知道如何使用正确的库并使代码矢量化对于科学计算至关重要。

我想补充一点,以常见的pythonic方法(面向对象的方法,列表,迭代器)处理大型数字数据集可能效率极低。在科学计算中,可能有必要以与大多数传统python编码器处理数据的方式截然不同的方式来构造代码。

If you’re in and using python for science (which it seems you are) part of that will be learning and understanding scientific libraries, for me these would be

  • numpy
  • scipy
  • matplotlib
  • mayavi/mlab
  • chaco
  • Cython

knowing how to use the right libraries and vectorize your code is essential for scientific computing.

I wanted to add that, handling large numeric datasets in common pythonic ways(object oriented approaches, lists, iterators) can be extremely inefficient. In scientific computing, it can be necessary to structure your code in ways that differ drastically from how most conventional python coders approach data.


回答 8

Google最近刚刚发布了在线Python类(“类”中的“类”)。

http://code.google.com/edu/languages/google-python-class/

我知道这不能解决您的全部问题,但是我认为这是一个不错的起点!

Google just recently released an online Python class (“class” as in “a course of study”).

http://code.google.com/edu/languages/google-python-class/

I know this doesn’t answer your full question, but I think it’s a great place to start!


回答 9

下载Twisted并查看源代码。他们采用了一些相当先进的技术。

Download Twisted and look at the source code. They employ some pretty advanced techniques.


回答 10

彻底理解所有数据类型和结构

对于每种类型和结构,编写一系列演示程序,以演示该类型或数据结构的各个方面。如果这样做,可能值得在每个博客上写笔记…这可能对很多人都有用!

Thoroughly Understand All Data Types and Structures

For every type and structure, write a series of demo programs that exercise every aspect of the type or data structure. If you do this, it might be worthwhile to blog notes on each one… it might be useful to lots of people!


回答 11

整个夏天,我只是通过在python网站上进行教程就自己独自学习了python(遗憾的是,我似乎再也找不到了,因此无法发布链接)。

后来,在大学的第一年类中,我被教给了python。在随后的夏天,我练习了PythonChallengeGoogle Code Jam的问题。从算法的角度以及从学习Python可以做什么以及如何操纵它以充分利用python的角度来看,解决这些问题都是有帮助的。

出于类似的原因,我听说高尔夫代码也可以使用,但是我从来没有亲自尝试过。

I learned python first by myself over a summer just by doing the tutorial on the python site (sadly, I don’t seem to be able to find that anymore, so I can’t post a link).

Later, python was taught to me in one of my first year courses at university. In the summer that followed, I practiced with PythonChallenge and with problems from Google Code Jam. Solving these problems help from an algorithmic perspective as well as from the perspective of learning what Python can do as well as how to manipulate it to get the fullest out of python.

For similar reasons, I have heard that code golf works as well, but i have never tried it for myself.


回答 12

学习算法/数学/文件IO / Python优化

这不会让您大开眼界,但首先请尝试解决Euler项目的问题 如果您拥有不错的中学数学并且知道如何使用Google,那么前50个左右的类不应向您征税。当您解决一个问题时,您可以进入论坛,在这里您可以浏览其他人的解决方案,这将教给您更多的知识。不过要保持体面,不要发布您的解决方案,因为这样做的目的是鼓励人们自己解决问题。

如果您使用蛮力算法,那么强迫自己使用Python工作将是无法原谅的。这将教您如何在内存中布置大型数据集,以及如何使用快速语言功能(例如字典)有效地访问它们。

通过自己做这个,我学到了:

  • 文件IO
  • 动态编程等算法和技术
  • Python数据布局
    • 字典/哈希图
    • 清单
    • 元组
    • 其各种组合,例如字典到元组列表
  • 生成器
  • 递归函数
  • 开发Python库
    • 文件系统布局
    • 在解释器会话期间重新加载它们

而且非常重要

  • 什么时候放弃并使用C或C ++!

所有这些都应该与生物信息学有关

诚然,我没有从该经验中学到Python的OOP功能。

Learning algorithms/maths/file IO/Pythonic optimisation

This won’t get you guru-hood but to start out, try working through the Project Euler problems The first 50 or so shouldn’t tax you if you have decent high-school mathematics and know how to Google. When you solve one you get into the forum where you can look through other people’s solutions which will teach you even more. Be decent though and don’t post up your solutions as the idea is to encourage people to work it out for themselves.

Forcing yourself to work in Python will be unforgiving if you use brute-force algorithms. This will teach you how to lay out large datasets in memory and access them efficiently with the fast language features such as dictionaries.

From doing this myself I learnt:

  • File IO
  • Algorithms and techniques such as Dynamic Programming
  • Python data layout
    • Dictionaries/hashmaps
    • Lists
    • Tuples
    • Various combinations thereof, e.g. dictionaries to lists of tuples
  • Generators
  • Recursive functions
  • Developing Python libraries
    • Filesystem layout
    • Reloading them during an interpreter session

And also very importantly

  • When to give up and use C or C++!

All of this should be relevant to Bioinformatics

Admittedly I didn’t learn about the OOP features of Python from that experience.


回答 13

您看过《使用Python进行生物信息学编程》一书吗?您似乎是其焦点小组的确切成员。

Have you seen the book “Bioinformatics Programming using Python“? Looks like you’re an exact member of its focus group.


回答 14

您已经有很多阅读材料,但是如果您能处理更多,我建议您阅读Python增强建议,尤其是“完成的” PEP和“推迟,放弃,撤回和拒绝”,以了解python的发展。 PEP。

通过了解语言的变化,做出的决定及其原理,您将了解Python的哲学,并了解“惯用的Python”是如何产生的。

http://www.python.org/dev/peps/

You already have a lot of reading material, but if you can handle more, I recommend you learn about the evolution of python by reading the Python Enhancement Proposals, especially the “Finished” PEPs and the “Deferred, Abandoned, Withdrawn, and Rejected” PEPs.

By seeing how the language has changed, the decisions that were made and their rationales, you will absorb the philosophy of Python and understand how “idiomatic Python” comes about.

http://www.python.org/dev/peps/


回答 15

使用Python 尝试http://challenge.greplin.com/


回答 16

与开始学习Python的人一起教书总是使您的想法更清晰的好方法,有时,我通常会从学生那里得到很多整洁的问题,让我重新思考关于Python的概念性问题。

Teaching to someone else who is starting to learn Python is always a great way to get your ideas clear and sometimes, I usually get a lot of neat questions from students that have me to re-think conceptual things about Python.


回答 17

不完全是您的要求,但我认为这是个好建议。

学习另一种语言,哪个都没关系。每种语言都有其自己的思想和约定,您可以从中学习。了解语言上的差异,更重要的是why它们是不同的。尝试使用像Haskell这样的纯函数式语言,看看没有副作用的函数的一些优点(和挑战)。了解如何将从其他语言中学到的东西应用到Python。

Not precisely what you’re asking for, but I think it’s good advice.

Learn another language, doesn’t matter too much which. Each language has it’s own ideas and conventions that you can learn from. Learn about the differences in the languages and more importantly why they’re different. Try a purely functional language like Haskell and see some of the benefits (and challenges) of functions free of side-effects. See how you can apply some of the things you learn from other languages to Python.


回答 18

我建议从一些可以迫使您探索语法表现力的东西开始。Python允许使用多种不同的方式来编写相同的功能,但是通常只有一种最优雅,最快的方法。如果您习惯于其他语言的习惯用法,那么您可能永远不会找到或接受这些更好的方法。我花了一个周末来研究前20个左右的Euler项目问题,并使用Django在Google App Engine上制作了一个简单的webapp。也许这只会使您从学徒带到新手,但您可以继续制作更高级的Web应用程序,并解决更高级的Euler项目问题​​。几个月后,我回过头来,在一个小时而不是一个周末的时间内从头解决了前20个PE问题。

I recommend starting with something that forces you to explore the expressive power of the syntax. Python allows many different ways of writing the same functionality, but there is often a single most elegant and fastest approach. If you’re used to the idioms of other languages, you might never otherwise find or accept these better ways. I spent a weekend trudging through the first 20 or so Project Euler problems and made a simple webapp with Django on Google App Engine. This will only take you from apprentice to novice, maybe, but you can then continue to making somewhat more advanced webapps and solve more advanced Project Euler problems. After a few months I went back and solved the first 20 PE problems from scratch in an hour instead of a weekend.