Python 有哪些重要的语言特性(习语)需要尽早学习
我有兴趣了解 StackOverflow 社区认为 Python 的重要语言特性(习语)是什么。 将程序员定义为 Pythonic 的特征。
Python (pythonic) 习语 - Python 语言自然的或特有的“代码表达式”。
另外,所有 Python 程序员都应该尽早学习哪些习惯用法?
提前致谢
相关:
I would be interested in knowing what the StackOverflow community thinks are the important language features (idioms) of Python. Features that would define a programmer as Pythonic.
Python (pythonic) idiom - "code expression" that is natural or characteristic to the language Python.
Plus, Which idioms should all Python programmers learn early on?
Thanks in advance
Related:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Python 是一种语言,可以描述为:
遵循相同的简单标准。一切都是可访问的、可更改的和可调整的。语言级别的元素非常少。
以 len(data) 内置函数为例。
len(data)< /code> 的工作原理是简单地检查
data.__len__()
方法,然后调用它并返回值,这样,len()
就可以对任何对象起作用。 ”首先学习类型和基本语法:
然后继续学习 python 的工作原理:
dir()
函数__builtins____len__
这样的一旦您了解了如何将各个部分组合在一起,请返回并涵盖一些更高级的语言功能:
一旦您对这些项目有了一定程度的了解(重点关注使它们变得Python化的原因),请查看更具体的项目:
并且永远不要忘记Python的禅宗(作者:Tim Peters)
Python is a language that can be described as:
Nearly everything in python follows the same simple standards. Everything is accessible, changeable, and tweakable. There are very few language level elements.
Take for example, the len(data) builtin function.
len(data)
works by simply checking for adata.__len__()
method, and then calls it and returns the value. That way,len()
can work on any object that implements a__len__()
method.Start by learning about the types and basic syntax:
Then move on to learning about how python works:
dir()
function__builtins__
Once you have an understanding of how to fit pieces together, go back and cover some of the more advanced language features:
__len__
(there are tons of these)And once you have a comfort level with these items (with a focus on what makes them pythonic), look at more specific items:
And never forget The Zen of Python (by Tim Peters)
本页涵盖了所有主要的 python 习惯用法: http://python.net /~goodger/projects/pycon/2007/idiomatic/handout.html
This page covers all the major python idioms: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
Python 中的一个重要习惯用法是文档字符串。
每个对象都有一个 __doc__ 属性,可用于获取有关该对象的帮助。 您可以在模块、类、方法和函数上设置 __doc__ 属性,如下所示:
现在,当您输入
help(m)
、help(mf)
等时,它会将文档字符串打印为帮助消息。因为它只是正常对象内省的一部分,所以它可以由 epydoc 等文档生成系统使用,或者由 unittest 用于测试目的。
它还可以用于更非常规(即非惯用)的用途,例如 Dparser。
对我来说更有趣的是,尽管 doc 在大多数对象上是只读属性,但您可以在任何地方使用它们,如下所示:
以及文档工具,例如 epydoc 可以拾取它们并正确格式化它们(而不是保留在代码格式中的普通注释。
An important idiom in Python is docstrings.
Every object has a __doc__ attribute that can be used to get help on that object. You can set the __doc__ attribute on modules, classes, methods, and functions like this:
Now, when you type
help(m)
,help(m.f)
etc. it will print the docstring as a help message.Because it's just part of normal object introspection this can be used by documention generating systems like epydoc or used for testing purposes by unittest.
It can also be put to more unconventional (i.e. non-idiomatic) uses such as grammars in Dparser.
Where it gets even more interesting to me is that, even though doc is a read-only attribute on most objects, you can use them anywhere like this:
and documentation tools like epydoc can pick them up and format them properly (as opposed to a normal comment which stays inside the code formatting.
装饰者得到我的投票。 您还可以在哪里编写类似以下内容
并获得如下输出:
Decorators get my vote. Where else can you write something like:
and get output like:
一切都与列表使用相关。
推导式、生成器等
Everything connected to list usage.
Comprehensions, generators, etc.
就我个人而言,我真的很喜欢使用缩进来定义代码块的 Python 语法,而不是使用“BEGIN”和“END”一词(如 Microsoft 的 Basic 和 Visual Basic - 我不喜欢这些)或通过使用左大括号和右大括号(如 C、C++、Java、Perl - 我喜欢这些)。
这真的让我感到惊讶,因为虽然缩进对我来说一直非常重要,但我并没有对此发出太大的“噪音”——我忍受着它,并且它被认为是一种能够阅读其他人的技能,“意大利面条” “ 代码。 此外,我从未听到其他程序员建议将缩进作为语言的一部分。 直到Python! 我只希望我首先意识到这个想法。
对我来说,Python 的语法就好像迫使你编写良好的、可读的代码。
好吧,我会离开我的肥皂盒。 ;-)
Personally, I really like Python syntax defining code blocks by using indentation, and not by the words "BEGIN" and "END" (as in Microsoft's Basic and Visual Basic - I don't like these) or by using left- and right-braces (as in C, C++, Java, Perl - I like these).
This really surprised me because, although indentation has always been very important to me, I didn't make to much "noise" about it - I lived with it, and it is considered a skill to be able to read other peoples, "spaghetti" code. Furthermore, I never heard another programmer suggest making indentation a part of a language. Until Python! I only wish I had realized this idea first.
To me, it is as if Python's syntax forces you to write good, readable code.
Okay, I'll get off my soap-box. ;-)
从更高级的角度来看,了解 Python 内部如何使用字典。 类、函数、模块、引用都只是字典上的属性。 一旦理解了这一点,就很容易理解如何进行猴子修补和使用强大的 __gettattr__、__setattr__ 和 __call__ 方法。
From a more advanced viewpoint, understanding how dictionaries are used internally by Python. Classes, functions, modules, references are all just properties on a dictionary. Once this is understood it's easy to understand how to monkey patch and use the powerful __gettattr__, __setattr__, and __call__ methods.
这是一个可以提供帮助的。 有什么区别
:和
答案:
在第二个示例中, foo 仅被调用一次。 如果 foo 有副作用,或者用于构造列表的可迭代对象很大,那么这可能很重要。
Here's one that can help. What's the difference between:
and
answer:
in the second example, foo is called only once. This may be important if foo has a side effect, or if the iterable being used to construct the list is large.
有两件事特别让我印象深刻,那就是动态类型和 Python 中使用的各种类型的列表,尤其是元组。
Python 对列表的痴迷可以说是 LISP-y,但它有自己独特的风格。 像这样的一行:
甚至
只是看起来像 Python 而没有其他东西。 (从技术上讲,你也会在 Lua 中看到后者,但 Lua 总体来说非常 Pythonic。)
Two things that struck me as especially Pythonic were dynamic typing and the various flavors of lists used in Python, particularly tuples.
Python's list obsession could be said to be LISP-y, but it's got its own unique flavor. A line like:
or even
just looks like Python and nothing else. (Technically, you'd see the latter in Lua as well, but Lua is pretty Pythonic in general.)
使用字符串替换:
当我不使用 python 编程时,这种简单的使用是我最怀念的。
Using string substitutions:
When I'm not programming in python, that simple use is what I miss most.
另一件你不能尽早开始的事情可能是测试。 在这里,文档测试尤其是通过同时解释代码来测试代码的好方法。
doctests 是简单的文本文件,包含交互式解释器会话以及如下文本:
如果 egatext 返回不同的内容,则测试将失败。
doctests 可以位于文档字符串或独立文本文件中,并使用 doctests 模块 执行。 当然,也可以使用更知名的单元测试。
Another thing you cannot start early enough is probably testing. Here especially doctests are a great way of testing your code by explaining it at the same time.
doctests are simple text file containing an interactive interpreter session plus text like this:
If e.g. a.text returns something different the test will fail.
doctests can be inside docstrings or standalone textfiles and are executed by using the doctests module. Of course the more known unit tests are also available.
我认为网上的教程和书籍只讲做事,而不是以最好的方式做事。 除了 python 语法之外,我认为在某些情况下速度也很重要。
Python 提供了一种对函数进行基准测试的方法,实际上是两种!!
一种方法是使用
profile
模块,如下所示:另一种方法是使用
timeit
模块,如下所示:I think that tutorials online and books only talk about doing things, not doing things in the best way. Along with the python syntax i think that speed in some cases is important.
Python provides a way to benchmark functions, actually two!!
One way is to use the
profile
module, like so:Another way to do this is to use the
timeit
module, like this: