内容简介:If a name is bound anywhere within a code block, all uses of the name within the block are treated as references to the current block. (Note: This can lead to errors when a name is used within a block before it is bound.).. code:: python坑就坑在它接住的异常不是
这些都是我在文档里阅读到或者是在网上看到的,不足以成文却值得注意的知识.
-
字符格式化: '%s'会先把参数转换成unicode然后再填充进去.
-
嵌套list comprehension:
[(i, f) for i in i_list for f in f_list] -
The name binding operations are argument declaration, assignment, class and function definition, import statements, for statements, and except clauses. Each name binding occurs within a block defined by a class or function definition or at the module level (the top-level code block).
If a name is bound anywhere within a code block, all uses of the name within the block are treated as references to the current block. (Note: This can lead to errors when a name is used within a block before it is bound.)
-
An assignment operation can only bind a name in the current scope or in the global scope.
-
filter(function, iterable)相当于(item for item in iterable if function(item))如果function为None的话,相当于(item for item in iterable if item). 另外,itertools.filterfalse() <http://localhost/py35/library/itertools.html#itertools.filterfalse>__ 可以把结果为false的过滤出来。 -
iter(object[, sentinel])要求 object 必须实现__iter__()或者__getitem__()之一,第二个参数是停止提示。 -
round(number, [ndigits])给小数截断:
.. code:: python
>>> round(10.12345, 2) 10.12
-
python 2 中的
hasattr比较坑,原因如下:The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an exception or not.)
坑就坑在它接住的异常不是 AttributeError
而是 Exception
-
sum(iterable, [start])可以接受字符串,数字等。For some use cases, there are good alternatives to sum(). The preferred, fast way to concatenate a sequence of strings is by calling ''.join(sequence). To add floating point values with extended precision, see math.fsum(). To concatenate a series of iterables, consider using itertools.chain(). -
type(name, bases, dict)可以用来动态创建类,其中name是类名,bases是基类, dict是类的__dict__属性
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
算法Ⅰ~Ⅳ(C++实现):基础、数据结构、排序和搜索
Sedgewick / 高等教育出版社 / 2002-1 / 49.00元
本书通过C++实现方案以简洁、直接的方式对书中的算法和数据结构进行表述,并向学生提供在实际应用中验证这种方法的手段。 本书广泛地论述了与排序、搜索及相关应用有关的基本数据结构和算法。覆盖了数组、链表、串、树和其他基本数据结构,更多地强调抽象数据类型(ADT)、模块化程序设计、面向对象程序设计和C++类。本书包括排序、选择、优先队列ADT实现和符号表ADT(搜索)实现,配有帮助学生学习计算......一起来看看 《算法Ⅰ~Ⅳ(C++实现):基础、数据结构、排序和搜索》 这本书的介绍吧!