Foreword:
It’s been almost ten years since I first got acquainted with Python as a
programming language. When I first learned Python many years ago,
it was with a little reluctance. I had been programming in a different
language before, and all of the sudden at work, I was assigned to a
different team where everyone used Python. That was the beginning
of my own Python journey.
至从我接触python已经过去了十年了。多年前,当我第一次学习python的时候,我心里还是有小情绪的。我之前都是使用其他的编程语言,但是在工作中,我突然间被派到了一个每个人都使用python的团队。这就是我python旅程开始的地方。
When I was first introduced to Python, I was told that it was going to
be easy, that I should be able to pick it up quickly. When I asked my
colleagues for resources for learning Python, all they gave me was a
link to Python’s official documentation. Reading the documentation
was confusing at first, and it really took me a while before I even felt
comfortable navigating through it. Often I found myself needing to
look for answers in StackOverflow.
当我第一次接触python,有人告诉我python很简单,所以我应该很快就掌握它。当我向我的同事寻求学习python的资源时,他们都给我指向python官方文档的链接。刚开始读完文档是有疑惑的,而且花了不少时间,我才能比较自如的通过文档进行查询。我还是经常性的需要去StackOverflow上寻求帮助。
Coming from a different programming language, I wasn’t looking for
just any resource for learning how to program or what classes and
objects are. I was looking for specific resources that would teach me
the features of Python, what sets it apart, and how writing in Python
is different than writing code in another language.
因为有其他编程语言的基础,我并不是去学习该如何编程或者什么是类,什么是对象这些。我只是在寻找能展现python不同之处的语言特性的专项资源,合用python编程与其他语言编程不同的地方。
It really has taken me many years to fully appreciate this language. As
I read Dan’s book, I kept thinking that I wished I had access to a book
like this when I started learning Python many years ago.
这确实花了我许多年去彻底的欣赏这门语言。当我读到dan的书的时候,我多么希望是我在多年前彷徨的时候找到了这本书。
For example, one of the many unique Python features that surprised
me at first were list comprehensions. As Dan mentions in the book,a tell of someone who just came to Python from a different language
is the way they use for-loops. I recall one of the earliest code review
comments I got when I started programming in Python was, “Why
not use list comprehension here?” Dan explains this concept clearly
in section 6, starting by showing how to loop the Pythonic way and
building it all the way up to iterators and generators.
举个栗子,在诸多的python语言特性中,最让我感到惊异的是列表推导式。正如dan在书中所提到的,一些人正是因为python的for循环的书写方式而转到了python。我想起来我很早时候的一次代码审查中我收到的一个评价,“为什么不使用列表推导式呢?”dan在第六章通过如何很python的去循环并且如何创建迭代器和生成器很清晰地解释了这个概念。
In chapter 2.5, Dan discusses the different ways to do string formatting
in Python. String formatting is one of those things that defy the
Zen of Python, that there should only be one obvious way to do things.
Dan shows us the different ways, including my favorite new addition
to the language, the f-strings, and he also explains the pros and cons
of each method.
在2.5章节中,dan讨论了不同的方法去实现字符串的格式化。字符串的格式化问题是违反了python之禅的问题之一,而后者显然应该是我们需要去践行的。dan向我们展现了不同的方式,包括我最喜欢的新特性,f方式,而且他还解释了各种方法的优点和缺点。
The Pythonic Productivity Techniques section is another great resource.
It covers aspects beyond the Python programming language,
and also includes tips on how to debug your programs, how to manage
the dependencies, and gives you a peek inside Python bytecode.
python化的生产力技术这一章也是极好的。它重复介绍了python这门编程语言并且涵盖了如何去调试自己的代码,怎么去管理依赖包的技巧,介绍了一下python的字节码。
It truly is an honor and my pleasure to introduce this book, Python
Tricks, by my friend, Dan Bader.
这是我的荣幸来为我的朋友dan做序。
By contributing to Python as a CPython core developer, I get connected
to many members of the community. In my journey, I found
mentors, allies, and made many new friends. They remind me that
Python is not just about the code, Python is a community.
Mastering Python programming isn’t just about grasping the theoretical
aspects of the language. It’s just as much about understanding and
adopting the conventions and best practices used by its community.
Dan’s book will help you on this journey. I’m convinced that you’ll be
more confident when writing Python programs after reading it.
— Mariatta Wijaya, Python Core Developer
以上感谢的话就不翻译了。