我很想知道StackOverflow社区认为Python的重要语言特性(成语)是什么.将程序员定义为Pythonic的功能.
Python(pythonic)习语 - "代码表达式",它是Python语言的自然或特征.
另外,所有Python程序员应该尽早学习哪些成语?
提前致谢
有关:
代码像Pythonista:惯用语Python
Python:我错过了什么吗?
gahooa.. 25
Python是一种可以描述为的语言:
"规则你可以用一大袋钩子放在手掌中".
python中几乎所有内容都遵循相同的简单标准.一切都是可访问,可更改和可调整的.语言级元素非常少.
以len(数据)内置函数为例. len(data)
只需检查一个data.__len__()
方法,然后调用它并返回值.这样,len()
可以在任何实现__len__()
方法的对象上工作.
首先了解类型和基本语法:
动态强类型语言
bool,int,float,string,list,tuple,dict,set
陈述,缩进,"一切都是对象"
基本功能定义
然后继续学习python的工作原理:
进口和模块(非常简单)
python路径(sys.path)
该dir()
功能
__builtins__
一旦您了解了如何将各个部分组合在一起,请返回并介绍一些更高级的语言功能:
迭代器
覆盖像__len__
(有很多这些)
列表理解和生成器
类和对象(一旦你知道一些规则,再一次非常简单)
python继承规则
一旦你对这些物品感到舒适(重点关注使它们变成pythonic的东西),看看更具体的物品:
在python中进行线程处理(注意全局解释器锁)
情境管理者
数据库访问
文件IO
插座
等等...
永远不要忘记Python的禅宗(Tim Peters)
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
它应该被编辑,所以杂散的引号不会搞砸着色! (3认同)
CTT.. 9
这个页面涵盖了所有主要的python习语:http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
Python是一种可以描述为的语言:
"规则你可以用一大袋钩子放在手掌中".
python中几乎所有内容都遵循相同的简单标准.一切都是可访问,可更改和可调整的.语言级元素非常少.
以len(数据)内置函数为例. len(data)
只需检查一个data.__len__()
方法,然后调用它并返回值.这样,len()
可以在任何实现__len__()
方法的对象上工作.
首先了解类型和基本语法:
动态强类型语言
bool,int,float,string,list,tuple,dict,set
陈述,缩进,"一切都是对象"
基本功能定义
然后继续学习python的工作原理:
进口和模块(非常简单)
python路径(sys.path)
该dir()
功能
__builtins__
一旦您了解了如何将各个部分组合在一起,请返回并介绍一些更高级的语言功能:
迭代器
覆盖像__len__
(有很多这些)
列表理解和生成器
类和对象(一旦你知道一些规则,再一次非常简单)
python继承规则
一旦你对这些物品感到舒适(重点关注使它们变成pythonic的东西),看看更具体的物品:
在python中进行线程处理(注意全局解释器锁)
情境管理者
数据库访问
文件IO
插座
等等...
永远不要忘记Python的禅宗(Tim Peters)
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
这个页面涵盖了所有主要的python习语:http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
Python中一个重要的习语是docstrings.
每个对象都有一个__doc__属性,可用于获取该对象的帮助.您可以在模块,类,方法和函数上设置__doc__属性,如下所示:
# this is m.py """ module docstring """ class c: """class docstring""" def m(self): """method docstring""" pass def f(a): """function f docstring""" return
现在,当你键入help(m)
,help(m.f)
等它会打印的文档字符串作为帮助信息.
因为它只是普通对象内省的一部分,所以可以通过文档生成系统(如epydoc)或单元测试用于测试目的.
它也可以用于更加非常规(即非惯用)用途,例如Dparser中的语法.
对我来说更有意思的是,即使doc是大多数对象的只读属性,你也可以在任何地方使用它们:
x = 5 """ pseudo docstring for x """
像epydoc这样的文档工具可以选择它们并正确格式化它们(而不是保留在代码格式中的普通注释).
装饰者得到我的投票.你还能在哪里写下类似的东西:
def trace(num_args=0): def wrapper(func): def new_f(*a,**k): print_args = '' if num_args > 0: print_args = str.join(',', [str(x) for x in a[0:num_args]]) print('entering %s(%s)' %(f.__name__,print_args)) rc = f(*a,**k) if rc is not None: print('exiting %s(%s)=%s' %(f.__name__,str(rc))) else: print('exiting %s(%s)' %(f.__name__)) return rc return new_f return wrapper @trace(1) def factorial(n): if n < 2: return 1 return n * factorial(n-1) factorial(5)
获得如下输出:
entering factorial(5) entering factorial(4) entering factorial(3) entering factorial(2) entering factorial(1) entering factorial(0) exiting factorial(0)=1 exiting factorial(1)=1 exiting factorial(2)=2 exiting factorial(3)=6 exiting factorial(4)=24 exiting factorial(5)=120